开发者

How to show two strings in ASP.NetDropDownList SelectList dataTextField?

开发者 https://www.devze.com 2023-01-22 23:37 出处:网络
This is my DropDownList in ASP.Net: <%: Html.DropDownListFor(x => x.CompanyUserFilterId, new SelectList(Model.CompanyUser开发者_JS百科s, \"Id\", \"FirstName\", Model.CompanyUserFilterId)) %>

This is my DropDownList in ASP.Net:

<%: Html.DropDownListFor(x => x.CompanyUserFilterId, new SelectList(Model.CompanyUser开发者_JS百科s, "Id", "FirstName", Model.CompanyUserFilterId)) %>

I want to list all CompanyUsers in the DropDownList and show both their FirstName and LastName property. CompanyUsers is an IEnumerable property. The code above works and shows only FirstName property. Is it possible to show both?


Instead of passing your complete customer data as the model, you will have to instead provide a projection of your data which contains the concatenated field.

You haven't provided your controller implementation but you probably want to do something like this:

var data = from c in DataContext select new CustomerData() { Id = c.Id, Name = c.FirstName + " " + c.LastName };

return View(data);

.. where CustomerData is a simple POCO with Id and Name properties on it. Then change your view to use IEnumerable<CustomerData> and your drop-down list code to use the 'Name' property from your projected model, instead of the FirstName property you had originally.

Hopefully you can tweak that to suit your own specific scenario.


I'll add that the MvcContrib's drop down lists is soo soo soo much better than the default one, I'd consider using that library if at all possible.

0

精彩评论

暂无评论...
验证码 换一张
取 消