Why is DropDownListFor not recognizing the selected value in my editor template?

人盡茶涼 提交于 2020-01-01 11:54:06

问题


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; }

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.

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


回答1:


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

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.



来源:https://stackoverflow.com/questions/10163443/why-is-dropdownlistfor-not-recognizing-the-selected-value-in-my-editor-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!