How can i Send my list From my controller to my Dr

2019-07-26 08:34发布

问题:

i know it should look like this

@Html.DropDownList("yourDropName", listItems, new { @class = "form-control" })

but i can't figure it out how should i do it in my controller i passed a list to it which was initialized in my model layer and in my View_Model i simply get it from model and pass it to my controller

this is in my model

    public List<int?> TeamId()
    {
        ListTeam = new List<int?>();
        using (var context = new EF_Model.DigikalaHREntities())
        {
            //var p = context.Members.Any(c => c.TeamId);
            var p = from c in context.Members
                    where c.TeamId.HasValue
                    select new { c.TeamId };
            return p.Select(m => m.TeamId).ToList();
        }
    } 

i have 2 Classes in my view model first one is just entities with some Data Annotation and second one is the class which i get my methods from my Model so this is my View model layer(i didn't passed this one to my View)

     public List<int?> GetTeamId()
    {
        Ref_CRUD = new Models.CRUD.Member();
        return Ref_CRUD.TeamId();
    } 

my controller

  #region [- Get -]
    public ActionResult Create()
    {
        Ref_Member = new View_Model.Buisiness.Member();
        return View(Ref_Member.GetTeamId());
    }

this is my View

@model DigikalaHR.View_Model.Entitys.Member
 . 
 .
 .
 .
 <div class="form-group">
        @Html.LabelFor(model => model.TeamId, "TeamId", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">       
 //don't know how to use one of These  two lines and which one i should use 
            @*@Html.DropDownList("TeamId",new SelectList(enum.(typeof())),htmlAttributes: new {@class="form-control" })*@
           @*@Html.DropDownListFor(m => m.TeamId,new SelectList(Enum.GetValues(typeof(id))),"GetTeamId")*@
            @Html.ValidationMessageFor(model => model.TeamId, "", new { @class = "text-danger" })
        </div>
    </div>

How can i Send my list From my controller to my DropDownList in the View?

回答1:

I think you are mixing things up here. Your view models should be models specific to the view and those are simply dumb POCO classes for data tranfer. No business logic or those has no idea about what your data access technology / entity classes are. Do not mix view models with entity classes.

So in your case, if your view is to create a member, you should have a view model with properties which are absolutely needed in the view (for input elements)

public class CreateMemberVm
{
   public string FirstName { set;get;}
   public int? SelectedTeamId { set;get;}
   public List<SelectListItem> Teams { set;get;}
   // Add other properties as needed by the view. 
}

Add other properties to your view model as needed by the view. There is no reason to blindly add all the properties from your entity class to your view models.

Now in your GET action, you initialize an object of this view model, popualte the Teams property with a list of SelectListItem objects (we will use this in the view to build the SELECT element options) and send the object to the vieww

public ActionResult Create()
{
   var vm = new CreateMemeberVm();
   vm.Teams = GetTeamItems();
   return View(vm);
}
public List<SelectListItem> GetTeamItems()
{
    using (var db = new EF_Model.DigikalaHREntities())
    {
        return db.Teams
                 .Select(a=>new SelectListItem { 
                                                 Value=a.TeamId.ToString(),
                                                 Text=a.TeamName
                                               })
                  .ToList();
    }
}

Now in your view, which should be strongly typed to our view model, you can use the DropDownListFor helper method to render the SELECT element.

@model CreateMemberVm
@using(Html.BeginForm())
{
   @Html.LabelFor(a=>a.FirstName)
   @Html.TextBoxFor(a=>a.FirstName)

   @Html.LabelFor(a=>a.SelectedTeamId)
   @Html.DropDownListFor(m => m.SelectedTeamId, Model.Teams, "Select team")
   <input type="submit" />
}

You can use the same view model class as the parameter of your HttpPost action method and now when user submits the form after filling the form, the SelectedTeamId property and the FirstName property will be populated from the form data and you can use those to save to your table

[HttpPost]
public ActionResult Create(CreateMemeberVm model)
{
  // check model.FirstName and model.SelectedId
  // to do : Save as needed
  // to do : return something
}

If you want to preselect an item in your view (for the case of edit member use case), all you have to do is, set the SelectedTeamId property of your view model to whatever you have in your db

public ActionResult Edit(int id)
{
   var m = db.Members.Find(id);
   var vm = new CreateMemeberVm() { FirstName = m.FirstName, SelectedTeamId=m.TeamId};
   vm.Teams = GetTeamItems();
   return View(vm);
}