Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 764
stromflut Avatar asked Nov 15 '22 15:11

stromflut


1 Answers

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.

like image 187
HokieMike Avatar answered Dec 05 '22 18:12

HokieMike