开发者

Selectlist and dropdownlist

开发者 https://www.devze.com 2022-12-23 02:59 出处:网络
I\'d like to create a dropdownlist from following structure List<KeyValuePair<long, string>> sponsori =

I'd like to create a dropdownlist from following structure

List<KeyValuePair<long, string>> sponsori = 
    new List<KeyValuePair<long, string>>();

Now I want the selectlist to have the pair's long as data value, th开发者_运维知识库e string as text value and the selected item, if I have only access to the long of the selected item.

THanks in advance.


In your action code

 yourViewModel.Sponsori= new SelectList(sponsori, "Key", "Value")

In your view code

<%=Html.DropDownList("yourSelectid", Model.Sponsori) %>


ViewData["selectList"] = new SelectList(sponsori, "Key", "Value");

And then on the page:

<%= Html.DropDownList("selectList") %>

You can also check out Rendering a Form in ASP.NET MVC Using HTML Helpers for a similar example (and more documentation).

0

精彩评论

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

关注公众号