ASP.net MVC how to get the value of the ID field for the list of items?

白昼怎懂夜的黑 提交于 2019-12-24 18:16:02

问题


I have an application, that shows list of items bound to a typed model.

In the list every item has an ID from a lookup table.

How to show the value of the lookup data for each item instead of IDs?

The best way has to link a html dropdownlist to each ID, from where I would be able to choose the appropriate value.

Thanks in advance

Gabrile


回答1:


Well the first step is to make sure you get all of the values that will be in the DropDownList. You should be able to easily fetch this from your database.

Set the text of the DropDownList items to the text value for the lookup item, and set the value to the ID of those same items.

When creating the DropDownList make sure to set the Selected one to be the option which matches the ID of your modeled data item.




回答2:


The answer I found here, that works:

asp.net mvc and linq to entities: how to include text value from IEnumerable<SelectListItem>

The only question remained for me: in that solution I can grab the value for the ID, but how to set a dropdownList selected value to the ID value?



来源:https://stackoverflow.com/questions/1832870/asp-net-mvc-how-to-get-the-value-of-the-id-field-for-the-list-of-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!