使用NHibernate,ASP.NET MVC和Dropdown

时间:2022-01-24 03:10:56

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?

我们将ASP.NET MVC与NHibernate一起使用,我们希望使用数据库中的值(自定义类)创建多个下拉列表。这实际上有效,但我如何处理“空”值;我的意思是,“未选择的”DropdownValue?列表顶部的“ - 选择 - ”之类的东西......有没有人有这种组合NHibernate,ASP.NET MVC和Dropdown的经验?

Thanks!

2 个解决方案

#1


1  

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.

我认为这可以通过使用ViewModel类轻松处理。假设您的应用中有一个OrderView,它会显示一个订单。在此视图中,您有一个下拉列表“项类型”,其中包含项类型列表。

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--".

应该提供给视图的模型应该是类似OrderViewModel的类,它包含ItemType对象的列表。当控制器使用NHib从DB加载ItemType对象列表时,它可以在集合中插入额外的“特殊”项类型,例如“--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.

换句话说,不要直接绑定到Nhibernate集合映射属性 - 使用Nhibernate将数据加载到一个列表中,然后您可以随意操作。

#2


0  

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

在Dropdown中获得“--select--”值的最佳方法是参数optionLabel,它可以在Html.DropdownList中找到

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

然后将“null”保存到您的数据库,你应该有一个干净的方式;)

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

#1


1  

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.

我认为这可以通过使用ViewModel类轻松处理。假设您的应用中有一个OrderView,它会显示一个订单。在此视图中,您有一个下拉列表“项类型”,其中包含项类型列表。

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--".

应该提供给视图的模型应该是类似OrderViewModel的类,它包含ItemType对象的列表。当控制器使用NHib从DB加载ItemType对象列表时,它可以在集合中插入额外的“特殊”项类型,例如“--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.

换句话说,不要直接绑定到Nhibernate集合映射属性 - 使用Nhibernate将数据加载到一个列表中,然后您可以随意操作。

#2


0  

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

在Dropdown中获得“--select--”值的最佳方法是参数optionLabel,它可以在Html.DropdownList中找到

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

然后将“null”保存到您的数据库,你应该有一个干净的方式;)

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