简体   繁体   English

使用NHibernate,ASP.NET MVC和Dropdown

[英]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. 我们将ASP.NET MVC与NHibernate一起使用,我们希望使用数据库中的值(自定义类)创建多个下拉列表。 This actually works, but how do I handle the "Empty" value; 这实际上有效,但我如何处理“空”值; I mean, the "unselected" DropdownValue? 我的意思是,“未选中的”DropdownValue? Something like "-- Select --" on the top of the List... Does anyone have experience with this combination NHibernate, ASP.NET MVC and Dropdown? 列表顶部的“ - 选择 - ”之类的东西......有没有人有这种组合NHibernate,ASP.NET MVC和Dropdown的经验?

Thanks! 谢谢!

I think this is easily handled by using a ViewModel class. 我认为这可以通过使用ViewModel类轻松处理。 Lets say you have an OrderView in your app, which displays an order. 假设您的应用中有一个OrderView ,它会显示一个订单。 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. 应该提供给视图的模型应该是类似OrderViewModel的类,它包含ItemType对象的列表。 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--". 当控制器使用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将数据加载到一个列表中,然后您可以随意操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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