【问题标题】:MVC DropDownListFor selected item not binding due to property name由于属性名称,MVC DropDownListFor 所选项目未绑定
【发布时间】:2014-12-04 21:32:29
【问题描述】:

我发现了 DropDownListFor 不会将视图模型中的值绑定到列表的一些边缘情况。

下面的代码不会绑定值。

视图模型

public class PersonViewModel
{
    [Required]
    public Title? Title { get; set; }
    public IEnumerable<SelectListItem> TitleSelectList { get; set; }
}

控制器

    public ActionResult Person()
    {
        return View(
             new PersonViewModel
             {
                 Title = Title.Mrs,
                 TitleSelectList = EnumHelper.GetSelectList(typeof(Title)),
             });
    }

查看

@Html.DropDownListFor(model => model.Title, Model.TitleSelectList, "Please select...", new { @class = "form-control" })

【问题讨论】:

    标签: c# asp.net-mvc html.dropdownlistfor


    【解决方案1】:

    更改属性名称可以解决此问题。

    将视图模型更改为 public Title? TitleTesting { get; set; } 并更新其余代码以使其匹配。

    我很想知道这是错误还是保留字。

    【讨论】:

    • 可能与ViewBag.Title冲突
    猜你喜欢
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多