1

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

在我的 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")
}

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

我不明白为什么,我将非常感谢您对此的帮助,谢谢您的帮助!

代码编辑后**

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

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

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

datavalueField 和 dataTextField 应该指的是什么?

4

1 回答 1

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 绑定到模型。

于 2013-02-20T16:44:13.163 回答