asp.net MVC 4.0 下拉列表中的选定值 [英] Selected value in asp.net MVC 4.0 dropdownlist

查看:35
本文介绍了asp.net MVC 4.0 下拉列表中的选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 DropdownListFor 函数制作一个下拉列表,我想在其中选择一个值.

I am trying to make a dropdown list using DropdownListFor function and I want to have a value selected in it.

我搜索了很多,找到了在 ViewModel 中有一个 SelectList 对象的类似解决方案,我尝试了这样的方法

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

在我的 ViewModel 中,我使用了一个 SelectList 类型的属性

In my ViewModel I used a property of type SelectList like this

SelectList HoursToSelect = new SelectList(MyDictionaryObject, "Value", "Key", 14/*selected value*/);

我就是这样使用它

@Html.DropDownListFor(m => m.EndsOnHour, HoursToSelect)

这一切都很好,完全满足我的要求

This all was working fine and fulfilled my requirement completely

但问题是由于我的项目的架构我不能在我的 ViewModel 中使用 SelectList 对象,因为我的项目不允许在 ViewModel<中使用不可序列化的类/代码>.

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?

推荐答案

当你使用 @Html.DropDownListFor(...) 和一个对象的属性时,它会使用对象的当前状态设置所选项目的对象.因此,在您的示例中,如果您未在选择列表中指定值,它将使用 m.EndsOnHour 的值来设置所选值.事实上,选择列表甚至不必是一个 SelectList,它实际上只需要是 SelectListItem 的集合,正如您在文档.

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 the m.EndsOnHour to set the selected value. In fact, the select list doesn't even really have to be a SelectList per say, it really just needs to be a collection of SelectListItems 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.

然而,如果列表是动态的,从长远来看这不会很好地工作,并且可能会导致额外的耦合,这将对系统的维护产生负面影响.在这种情况下,您需要创建自己的可序列化的 SelectList 子类,然后将其用作 IEnumerable 的可序列化实现的泛型类型参数.C# 中的数组是可序列化的,因此您可以处理这部分.

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 of IEnumerable<T>. Arrays in C# are serializable, so you that part is taken care of.

这篇关于asp.net MVC 4.0 下拉列表中的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆