Combine three values into one in a MVC dropdownlist?
I'm trying to combine the values Fir开发者_StackOverflow社区stName + LastName + Email into a single value for my dropdownlist in a MVC application using linq2sql.
The code I'm using now, that works for only one output value in the list, is:
<%= Html.DropDownList("SaleEmployeeId", new SelectList(ViewData["SaleEmployees"] as IEnumerable, "Id", "FirstName", Model.SaleEmployeeId), "-- Choose --", new { @class = "required" })%>
But I want to combine "FirstName + LastName + Email" where "FirstName" has its place in my dropdownlist.
Does anyone have a solution for this?
You could try this (in your controller):
var values = _userRepository.FindAllUsers().Select(u => new { ID = u.UserName, Name = u.FirstName + " " + u.LastName + " " + u.EmailAddress});
var userDropDown = new SelectList(values, "Id", "Name");
Then put userDropDown
in the viewbag/viewdata, and read it in your view.
精彩评论