简体   繁体   中英

ASP.net MVC 4 Drop Down List

I am a bit lost on the concept of drop down list in MVC 4.

I have to models

Gender - int ID - string Title

User -string FirstName - Gender -...

I am bit unsure how to tackle it, to keep it clean and simple.

For the type in my class users, should it be Gender because its the name of the table or should it be this IEnumerable?

If its if so i am missing other things?

Also should i provide something specific to the controllers? or how do I display the dropdownlist in C# MVC 4 Razor code.

Have a look at the SelectList class http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlist%28v=vs.108%29.aspx

This goes in your model, then you can create a dropdownlist like this:

@Html.DropDownList("GenderId", Model.Genders)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM