在asp.net 4.0 MVC DropDownList中选定的值 [英] Selected value in asp.net MVC 4.0 dropdownlist

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

问题描述

我试图让使用 DropdownListFor 功能的下拉列表,然后我想在它所选的值。

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

我寻觅了一番,发现类似一种有一个的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

在我的视图模型我用类型的属性的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

但问题是由于我的项目的建筑我不能使用的SelectList 对象我的视图模型我的项目呢不允许在非序列化的CLASSE 视图模型

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&LT的一般类型参数; T> 。在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 4.0 MVC DropDownList中选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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