简体   繁体   中英

Working with NHibernate, ASP.NET MVC and Dropdown

We're using ASP.NET MVC together with NHibernate and we'd like to create several Dropdown Lists using Values (custom Class) from the Database. This actually works, but how do I handle the "Empty" value; I mean, the "unselected" DropdownValue? Something like "-- Select --" on the top of the List... Does anyone have experience with this combination NHibernate, ASP.NET MVC and Dropdown?

Thanks!

I think this is easily handled by using a ViewModel class. Lets say you have an OrderView in your app, which displays an order. On this view you have a dropdown "Item Type", containing a list of item types.

The model that should be fed to your view should be a class like OrderViewModel which contains a list of ItemType objects. When the controller loads the list of ItemType objects from the DB using NHib, it can then insert extra "special" Item Types into the collection, such as one for "--SELECT--".

In other words, don't bind directly to an Nhibernate collection mapping property -- use Nhibernate to load data into a list which you can then manipulate at will.

Best way to have a "--select--" value in Dropdown will be the parameter optionLabel which is available in Html.DropdownList

Then save "null" to your database and you should have a clean way ;)

<%= Html.DropDownList("myDropdown",mylist,"--please choose--" %>

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