I am trying to create a list manually and then send that over to the view:
I have the following
var MyList = new[] {
new ListEntry {Name = "John Moore"},
new ListEntry {Name = "Jim Baker"}
};
ViewBag.MyList = new SelectList(MyList , "Name");
In the View, I have the following:
@Html.DropDownListFor(model => model.name.UserName, (IEnumerable<SelectListItem>)ViewBag.MyList)
I was expecting to see John Moore and Jim Baker in the list but instead the list has 2 rows with the following: Digit1.Controllers.ListEntry
Any idea on how I can the list list to show the names. Also when I do a submit, I also need the name to be
updated in the table (ex: John Moore or Jim Baker)
You’re using the wrong overload for
SelectList. You need to use the overload that tells it which Display/Value to use:Value and Name in this case are the names of the properties on your object that you want to use for the value and display field for your select list.
The overload you are using is this:
Basically you are saying your SelectList has “Name” as the selected (default) value.