【问题标题】:DropDownListFor Inside EditorFor Not Selecting ValuesDropDownListFor Inside EditorFor 不选择值
【发布时间】:2013-12-05 02:06:57
【问题描述】:

我发现了几个关于我的问题的问题,但没有一个真正解决了问题并给出了问题的替代解决方案,这就是我再次问这个问题的原因。

我正在使用强类型的 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();
    }
}

上面的视图模型使用下面的视图模型,它基本上保存了 3 个下拉列表的数据,即日、月和年。

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查看的action方法中,我设置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 不幸的是我没有。我改用了局部视图。
  • 谢谢。似乎它仍然是一个错误。

标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:

这似乎是一个老错误,尚未解决。这里有报道。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      • 2010-12-27
      • 1970-01-01
      相关资源
      最近更新 更多