为什么DropDownListFor不承认在我的编辑模板选择的值? [英] Why is DropDownListFor not recognizing the selected value in my editor template?

查看:114
本文介绍了为什么DropDownListFor不承认在我的编辑模板选择的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为DropDown.cshtml下面编辑模板。在列表部分工作正常,以及模板使用一些巫术我没有获得所需要的的SelectList ViewData的。该控制器放在视图模型中的所有选择列表进入的ViewData ,并没有什么不妥的事情的清单一边。

I have the following editor template called 'DropDown.cshtml'. The list part works fine, and the template uses some voodoo I did to get the required SelectList from ViewData. The controller places all select lists in the view model into ViewData, and there is nothing wrong with the list side of things.

@{
    var list = this.GetModelSelectList();
}
@Html.DropDownListFor(m => Model, list)

我使用外键视图模型属性此模板像这样的:

I use this template on foreign key view model properties like this one:

[Required]
[UIHint("DropDown", "MVC", "SelectListName", "JobLevelSelectList")]
[Display(Name = "Job Level")]
public Guid? JobLevelId { get; set; }

public SelectList JobLevelSelectList { get; set; }

在控制器中, JobLevelId 具有正确的价值立即执行的观点之前,还没有项目就在渲染选择元素。或者说,在选择列表中的第一项总是被选中。

In the controller, JobLevelId has the correct value immediately before executing the view, yet no item it selected in the rendered select element. or rather, the first item in the select list is always selected.

为什么 DropDownListFor 忽略的属性值时直接援引我的编辑模板中使用,但做工精细什么时候?

Why does DropDownListFor ignore the property value when used in my editor template and yet work fine when invoked directly?

推荐答案

这是不幸的是<一个href=\"http://connect.microsoft.com/VisualStudio/feedback/details/654543/asp-net-mvc-possible-mvc-bug-when-working-with-editortemplates-and-drop-down-lists#details\"相对=nofollow> MVC3中的一个已知的bug (我没有在MVC试过4测试版,看看它是否固定)。

This is unfortunately a known bug in MVC3 (I haven't tried it in MVC 4 Beta to see if it fixed).

身边,我已经使用的工作是手动设置的选择相应在该DropDownListFor势必集合属性,这是不理想,但它的工作。

The work around that I have used is to manually set the Selected property accordingly in collection that the DropDownListFor is bound to, it is not ideal but it worked.

这篇关于为什么DropDownListFor不承认在我的编辑模板选择的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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