DropDownListFor 在 EditorFor 中无法选择值

5

我发现了几个关于我的问题的问题,但没有一个实际上解决了问题并提供了解决方案,这就是为什么我再次提问的原因。

我正在使用强类型的HTML助手,而且只在页面标题中使用ViewData和ViewBag。

这是我的问题。

我有以下的视图模型。

public class RegisterViewModel
{

    public string Mail                      { get; set; }
    public string Name                      { get; set; }

    public ThreePartDatePickerViewModel ThreePartDateSelection { get; set; }

    public RegisterViewModel()
    {
        ThreePartDateSelection = new ThreePartDatePickerViewModel();
    }
}

上面的视图模型使用下面的视图模型,基本上保存了三个下拉列表的数据,它们是“日”,“月”和“年”。
public class ThreePartDatePickerViewModel
{
    public string Day              { get; set; }
    public string Year             { get; set; }
    public string Month            { get; set; }

    public IList<SelectListItem> Years      { get; set; }
    public IList<SelectListItem> Months     { get; set; }
    public IList<SelectListItem> Days       { get; set; }

    public ThreePartDatePickerViewModel()
    {
        var now = DateTime.Now;

        Years = new List<SelectListItem>();
        Months = new List<SelectListItem>();
        Days = new List<SelectListItem>();

        var empty = new SelectListItem { Text = "" };
        Years.Add(empty);
        Months.Add(empty);
        Days.Add(empty);

        foreach(var item in Enumerable.Range(0, 100).Select(x => new SelectListItem { Value = (now.Year - x).ToString(), Text = (now.Year - x).ToString() }))
            Years.Add(item);

        foreach(var item in Enumerable.Range(1, 12).Select(x => new SelectListItem { Value = x.ToString(), Text = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x) }))
            Months.Add(item);

        foreach(var item in Enumerable.Range(1, 31).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() }))
            Days.Add(item);


    }
}

在返回RegisterViewModel视图的操作方法中,我正在设置ThreePartDatePickerViewModel的Day、Month和Year属性。我在生成视图时运行时检查这些值,并且它们是正确的。
在我的主视图中,
@model Bla.Bla.RegisterViewModel

<!-- Helpers for other properties -->

@Html.EditorFor(m => m.ThreePartDateSelection)

我的ThreePartDatePickerViewModel编辑模板是:

@model Bla.Bla.ThreePartDatePickerViewModel

<div class="threePartDatePicker">

    @Html.DropDownListFor(m => m.Day, Model.Days)  
    @Html.DropDownListFor(m => m.Month, Model.Months)
    @Html.DropDownListFor(m => m.Year, Model.Years)

</div>

在我的最终渲染的HTML中,所有控件都如预期一样显示。下拉框呈现良好,除了我在操作方法中设置的值没有被选中。如果我从EditorTemplates切换到Partial Views,它就开始工作。或者如果直接在主视图中呈现下拉列表,而不是将模型传递给EditorFor,它也可以正常工作。

请问,您是否得到了关于这个问题的任何信息或解决方案? - Shady
很不幸,我没有使用它。我使用了局部视图。 - emre nevayeshirazi
谢谢。看起来这仍然是一个 bug。 - Shady
2个回答

4
似乎是一个旧的bug,尚未解决。在这里有报告。

http://connect.microsoft.com/VisualStudio/feedback/details/654543/asp-net-mvc-possible-mvc-bug-when-working-with-editortemplates-and-drop-down-lists#details

我必须编辑编辑器模板来设置选定的值为,
@{
    //capture the selected list wherever it is. Here it passed in additionalViewData
    SelectList tmpList, list = null;
    ViewContext.ViewData.TryGetValue("list", out tmpList);

    if (tmpList != null && tmpList.Count() > 0  && tmpList.SelectedValue == null)
    {
        list = new SelectList(tmpList, "Value", "Text", Model);
    }
    else
    {
        list = tmpList;
    }
}

<div class="form-group">
    @Html.DropDownListFor(m => m, list, attributes)
</div>

我希望默认情况下,可以在编辑器模板中使用选择列表。


2

我使用编辑模板中的下拉列表遇到了同样的问题。尽管我可以在模型中看到正确的值,但所选的值未设置。这是我的解决方法。它可以扩展到与其他DropdownListFor方法一起使用。

public static class HtmlHelperExtensions
{        
    public static IHtmlString EditorDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel)
    {
        var dropDown = SelectExtensions.DropDownListFor(htmlHelper, expression, selectList, optionLabel);
        var model = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model;
        if (model == null)
        {
            return dropDown;
        }

        var dropDownWithSelect = dropDown.ToString().Replace("value=\"" + model.ToString() + "\"", "value=\"" + model.ToString() + "\" selected");
        return new MvcHtmlString(dropDownWithSelect);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接