ASP.NET MVC DropDownListFor not select value from model [英] ASP.NET MVC DropDownListFor not selecting value from model

查看:22
本文介绍了ASP.NET MVC DropDownListFor not select value from model的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ASP.NET MVC 3,并且在使用 DropDownListFor HTML Helper 时遇到了问题".

I'm using ASP.NET MVC 3, and just ran into a 'gotcha' using the DropDownListFor HTML Helper.

我在控制器中执行此操作:

I do this in my Controller:

ViewBag.ShippingTypes = this.SelectListDataRepository.GetShippingTypes();

还有 GetShippingTypes 方法:

public SelectList GetShippingTypes()
{
    List<ShippingTypeDto> shippingTypes = this._orderService.GetShippingTypes();

    return new SelectList(shippingTypes, "Id", "Name");
}

我将它放在 ViewBag 而不是模型中的原因(我为每个视图都设置了强类型模型),是因为我有一个使用 EditorTemplate 呈现的项目集合,它也需要访问 ShippingTypes 选择列表.

The reason I put it in the ViewBag and not in the model (I have strongly typed models for each view), is that I have a collection of items that renders using an EditorTemplate, which also needs to access the ShippingTypes select list.

否则我需要遍历整个集合,然后分配一个 ShippingTypes 属性.

Otherwise I need to loop through the entire collection, and assign a ShippingTypes property then.

到目前为止一切顺利.

在我看来,我这样做:

@Html.DropDownListFor(m => m.RequiredShippingTypeId, ViewBag.ShippingTypes as SelectList)

(RequiredShippingTypeIdInt32 类型)

发生的情况是,RequiredShippingTypeId 的值在下拉列表中被选中.

What happens is, that the value of RequiredShippingTypeId is not selected in the drop down.

我遇到了这个:http://web.archive.org/web/20090628135923/http://blog.benhartonline.com/post/2008/11/24/ASPNET-MVC-SelectList-selectedValue-Gotcha.aspx

He suggests that MVC will lookup the selected value from ViewData, when the select list is from ViewData.我不确定是否是这种情况,因为博文已经很旧了,而且他在谈论 MVC 1 测试版.

He suggests that MVC will lookup the selected value from ViewData, when the select list is from ViewData. I'm not sure this is the case anymore, since the blog post is old and he's talking about MVC 1 beta.

解决此问题的解决方法是:

A workaround that solves this issue is this:

@Html.DropDownListFor(m => m.RequiredShippingTypeId, new SelectList(ViewBag.ShippingTypes as IEnumerable<SelectListItem>, "Value", "Text", Model.RequiredShippingTypeId.ToString()))

我尽量不在最后的 RequiredShippingTypeId 上使用 ToString,这给了我与以前相同的行为:未选择任何项目.

I tried not to ToString on RequiredShippingTypeId at the end, which gives me the same behavior as before: No item selected.

我认为这是一个数据类型问题.最终,HTML 助手将字符串(在选择列表中)与 Int32(来自 RequiredShippingTypeId)进行比较.

I'm thinking this is a datatype issue. Ultimately, the HTML helper is comparing strings (in the Select List) with the Int32 (from the RequiredShippingTypeId).

但是为什么在将 SelectList 放入 ViewBag 时它不起作用 - 当它在将其添加到模型时完美运行,并在视图中执行此操作时:

But why does it not work when putting the SelectList in the ViewBag -- when it works perfectly when adding it to a model, and doing this inside the view:

@Html.DropDownListFor(m => m.Product.RequiredShippingTypeId, Model.ShippingTypes)

推荐答案

之所以不起作用是因为 DropDownListFor 助手的限制:它能够推断选定的值使用作为第一个参数传递的 lambda 表达式仅当此 lambda 表达式是一个简单的属性访问表达式.例如,由于编辑器模板,这不适用于数组索引器访问表达式.

The reason why this doesn't work is because of a limitation of the DropDownListFor helper: it is able to infer the selected value using the lambda expression passed as first argument only if this lambda expression is a simple property access expression. For example this doesn't work with array indexer access expressions which is your case because of the editor template.

你基本上有(不包括编辑器模板):

You basically have (excluding the editor template):

@Html.DropDownListFor(
    m => m.ShippingTypes[i].RequiredShippingTypeId, 
    ViewBag.ShippingTypes as IEnumerable<SelectListItem>
)

不支持以下内容:m =>m.ShippingTypes[i].RequiredShippingTypeId.它仅适用于简单的属性访问表达式,但不适用于索引集合访问.

The following is not supported: m => m.ShippingTypes[i].RequiredShippingTypeId. It works only with simple property access expressions but not with indexed collection access.

您找到的解决方法是解决此问题的正确方法,即在构建 SelectList 时显式传递所选值.

The workaround you have found is the correct way to solve this problem, by explicitly passing the selected value when building the SelectList.

这篇关于ASP.NET MVC DropDownListFor not select value from model的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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