I am trying to add a Dropdownlistfor to my MVC Project with no luck. I would like to display a list of customers. I would like the customers displayed and the ID selected.
I am struggling with casting my list into the selectListItems, Could someone please help me out with this.
Controller
public ActionResult createNewUser()
{
List<string> Customers = DBH.CustomerBranchGetAll();
var Model = new UserModel
{
Customers = Customers
};
return View(Model);
}
Model
public class UserModel
{
public int CustomerId { get; set; }
public List<string> Customers { get; set; }
public IEnumerable<SelectListItem> SelectCustomers
{
get { return new SelectList(Customers, "Id", "Name"); }
}
}
View
<div class="editor-label">
@Html.Label("Choose Your Customer name")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.CustomerId, Model.SelectCustomers)
@Html.ValidationMessageFor(model => model.Customers)
</div>
public IEnumerable<SelectListItem> SelectCustomers { get { return new SelectList(Customers, Customers.Id, Customers.Name); } }stringhas no Properties calledIdandNameDBH.CustomerBranchGetAll();return a list of string (and not a list of Customers / something with an Id and a Name) ?