asp.net MVC 4.0 下拉列表中的选定值
我正在尝试使用 DropdownListFor 函数创建一个下拉列表,并且我想在其中选择一个值。
我进行了很多搜索,发现在 ViewModel
中有一个 SelectList
对象的类似解决方案,我
在我的 ViewModel
中 尝试了类似的方法> 我使用了 SelectList
类型的属性,就像这样
SelectList HoursToSelect = new SelectList(MyDictionaryObject, "Value", "Key", 14/*selected value*/);
,并且我像这样使用它
@Html.DropDownListFor(m => m.EndsOnHour, HoursToSelect)
这一切都工作正常并完全满足我的要求,
但问题是由于我的项目架构,我无法使用 SelectList
我的对象ViewModel
因为我的项目不允许 ViewModel
中存在不可序列化的类。
所以留给我的唯一方法就是只在我的视图中拥有一些东西,这里有一些东西
@Html.DropDownListFor(m => m.EndsOnHour, new SelectList(Model.Hours, "Value", "Key"))
但不知道是什么! 有人有想法来实现这一目标吗?
I am trying to make a dropdown list using DropdownListFor
function and I want to have a value selected in it.
I have searched a lot and found the similar kind of solution of having a SelectList
object in the ViewModel
, I tried it something like this
In my ViewModel
I used a property of type SelectList
like this
SelectList HoursToSelect = new SelectList(MyDictionaryObject, "Value", "Key", 14/*selected value*/);
and I was using it like this
@Html.DropDownListFor(m => m.EndsOnHour, HoursToSelect)
This all was working fine and fulfilled my requirement completely
But the problem is due to my project's architecture I cannot use SelectList
object in my ViewModel
as my project does not allow non serializable classe in the ViewModel
.
So the only way left for me is to have something in my View only, something here
@Html.DropDownListFor(m => m.EndsOnHour, new SelectList(Model.Hours, "Value", "Key"))
But don't know what!
Anybody have idea to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您将
@Html.DropDownListFor(...)
与对象的属性一起使用时,它将使用该对象的当前状态来设置所选项目。因此,在您的示例中,如果您未在选择列表中指定值,它将使用m.EndsOnHour
的值来设置所选值。事实上,选择列表甚至不必是一个SelectList
,它实际上只需要是SelectListItem
的集合,正如您在文档。但是,您的序列化问题仍然存在。这里有几个选项,其中最简单的就是将值放在视图侧(如果可能的话)。许多下拉列表都是为静态选择而构建的,以显示枚举的可能选择,例如列出州或国家/地区。其他常见的示例是日期范围等。在这些情况下,您可以编写帮助程序来生成这些列表并在视图中调用它们,然后将它们从视图模型中删除。
然而,如果列表是动态的,从长远来看,这不会很好地工作,并且可能会导致额外的耦合,从而对系统的维护产生负面影响。在这种情况下,您需要创建自己的可序列化
SelectList
子类,然后将其用作IEnumerable
可序列化实现的通用类型参数。 C# 中的数组是可序列化的,因此您可以处理该部分。When you use
@Html.DropDownListFor(...)
with a property of an object, it will use the current state of the object to set the selected item. So in your example, if you don't specify the value in the select list, it would use the value of them.EndsOnHour
to set the selected value. In fact, the select list doesn't even really have to be aSelectList
per say, it really just needs to be a collection ofSelectListItem
s as you can see in the documentation.Your problem of serialization still exists however. You have a couple options here, the easiest of which is to just throw the values on the view side, if that's possible. Many drop down lists are built for static choices to display possible selections of an enumeration, like listing off states or countries for instance. Other common examples are for date ranges, etc. In these instances, you can write helpers that will generate those lists and call them in your view, then just remove them from your view model.
If the list is dynamic however, this won't work very well in the long run and will likely cause extra coupling that will have a negative effect on the maintenance of your system. In this case you will need to create your own
SelectList
child class that is serializable and then use that as the generic type parameter of a serializable implementation ofIEnumerable<T>
. Arrays in C# are serializable, so you that part is taken care of.就我个人而言,我放弃了 DropDownListFor 并只是做了类似的事情......它简单得多。不确定是否有任何理由表明这不是一个好主意。
Personally I gave up with the DropDownListFor and just did something like this.... Its much simpler. Not sure if there's any reason why this would not be a good idea.