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?
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)
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 ofSelectListItem
objects (we will use this in the view to build the SELECT element options) and send the object to the viewwNow in your view, which should be strongly typed to our view model, you can use the
DropDownListFor
helper method to render the SELECT element.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 theFirstName
property will be populated from the form data and you can use those to save to your tableIf 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