Modelstate验证始终使用下拉列表返回false

时间:2013-02-20 16:35:40

标签: c# asp.net-mvc drop-down-menu

注意:代码已更新,以考虑下面的评论和回答。

在我的MVC应用程序中,我有时需要引用其他对象(如多对多关系或一对多关系)。

所以我有这个模型:

public class ObjInfo
    {
        [Required(ErrorMessage = "Obj ID is required.")]
        [Display(Name = "ObjID")]
        public int m_Id { get; set; }

        [Required(ErrorMessage = "Obj Name is required.")]
        [Display(Name = "Obj Name")]
        public string m_Name { get; set; }

        [Display(Name = "Obj Number")]
        public int m_Number { get; set; }

        (...)

        [Required(ErrorMessage = "Other Obj is required.")]
        [Display(Name = "OtherObj")]
        public int m_OtherObjID { get; set; }

        public OtherObjInfo m_OtherObj { get; set; }

        (...)
    }

我也有默认和参数构造函数,可以根据需要显示它们,但我不确定它们是否有错。反正。

在我的控制器中,我有两个跟随MVC方法的创建方法:

        //
        // GET: /Obj/Create

        public ActionResult Create()
        {
            ViewBag.List = new SelectList(PopulateDDLs(), "OtherObj", "Other obj ID");
            return View();
        }

        //
        // POST: /Obj/Create

        [HttpPost]
        public ActionResult Create(Objinfo obj)
        {
            if (ModelState.IsValid)
            {
                m_ObjManager.CreateObj(obj);
                return RedirectToAction("SearchIndex");
            }

            ViewBag.List = new SelectList(PopulateDDLs(), "OtherObj", "Other obj ID");
            return View(obj);
        }

最后,这就是我的“创建”视图的编码方式:

@model MyApp.Models.ObjInfo

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>OBJ</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.m_Id)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.m_Id)
            @Html.ValidationMessageFor(model => model.m_Id)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.m_Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.m_Name)
            @Html.ValidationMessageFor(model => model.m_Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.m_Number)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.m_Number)
            @Html.ValidationMessageFor(model => model.m_Number)
        </div>

        (...)

        <div class="editor-label">
            @Html.LabelFor(model => model.m_OtherObj , "Other Obj")
        </div>
        <div class="editor-field">
            @Html.DropDownList(model => model.m_OtherObjID, ViewBag.List as SelectList, "--- Select Other Obj ---", new {@class = "OtherObjInfo "})
            @Html.ValidationMessageFor(model => model.m_OtherObj)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

基本上,问题在于每次单击“创建”按钮时,即使在下拉列表中选择了某些内容,ModelO验证对于OtherObj也始终为false。除此之外,其他所有值都是正确的。

我不明白为什么,我非常感谢你对这个的帮助,谢谢你的帮助!

代码编辑后**

现在,当我进入“创建”视图时,我遇到了崩溃:

DataBinding: 'System.String' does not contain a property with the name 'OtherObj'.

正好在我的dropdownlistfor所在的行上。

datavalueField和dataTextField应该引用什么?

1 个答案:

答案 0 :(得分:1)

otherObjectId添加到您的模型

public class ObjInfo
{
    public int m_Id { get; set; }

    ...

    [Required(ErrorMessage = "Other Obj is required.")]
    [Display(Name = "OtherObj")]
    public int otherObjectId { get; set; }

    public OtherObjInfo m_OtherObj { get; set; }

    ...

}

控制器

public ActionResult Create()
{
    ViewBag.List = new SelectList(PopulateDDLs(), "Id", "Name"); 
    return View();
}

[HttpPost]
public ActionResult Create(Objinfo obj)
{
    if (ModelState.IsValid)
    {
        m_ObjManager.CreateObj(obj);
        return RedirectToAction("SearchIndex");
    }

    ViewBag.List = new SelectList(PopulateDDLs(), "Id", "Name"); 
    return View(obj);
}

视图

<div class="editor-label">
    @Html.LabelFor(model => model.m_OtherObj , "Other Obj")
</div>
<div class="editor-field">
    @Html.DropDownListFor(model => model.otherObjectId, ViewBag.List as SelectList, "--- Select Category ---", new { @class = "some_class" })
    @Html.ValidationMessageFor(model => model.m_OtherObj)
</div>

更好的方法是使用强类型助手。你的所有助手都是strongly-typed(editorfor,textboxfor,dropdownlistfor,...),下拉名单除外。

如果要将DDL值绑定到模型,则应使用dropdownlistfor而不是dropdownlist

您的模型状态无效,因为您没有将所需的值作为DDL绑定到模型。