0

这是一个非常普遍的问题,但我被卡住了!

我有一个模型类,我想在视图中编辑它:

public class FormFillModel
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }

    public string Sector { get; set; }
    public List<FormFieldModel> Fields { get; set; }
}

我可以编辑字符串属性,但不能编辑字段属性。

我为它创建了一个编辑器模板。在 ~/shared/editortemplates/FormFieldsListModel.cshtml 下

@model List&lt;SoruCevapForm.Models.FormFieldModel&gt;

@foreach (var field in Model)
{
<div class="editor-label">
    @Html.Label(field.Text)
</div>
<div class="editor-field">
    @Html.TextBoxFor(model => field.Val)
</div>
<br />
}

最后...

<h2>FillForm</h2>
@using (Html.BeginForm())
{
    <fieldset>
        <legend>@Model.Title</legend>
        @Html.HiddenFor(model => model.Id)
        <table>
            <tr>
                <td>
                    <div class="display-label">
                        @Html.DisplayNameFor(model => model.Title)
                    </div>
                </td>
                <td>
                    <div class="display-field">
                        @Html.DisplayFor(model => model.Title)
                    </div>
                </td>
            </tr>
            <tr>
                <td>
                    <div class="display-label">
                        @Html.DisplayNameFor(model => model.Sector)
                    </div>
                </td>
                <td>
                    <div class="display-field">
                        @Html.DisplayFor(model => model.Sector)
                    </div>
                </td>
            </tr>
            <tr>
                <td>
                    <div class="display-label">
                        @Html.DisplayNameFor(model => model.Description)
                    </div>
                </td>
                <td>
                    <div class="display-field">
                        @Html.DisplayFor(model => model.Description)
                    </div>
                </td>
            </tr>
        </table>
    </fieldset>

    <fieldset>
        <legend>Fields</legend>

        @Html.EditorFor( model => model.FieldsList.Fields, "FormFieldsListModel")

        <p>
            <input type="submit" value="Submit" />
        </p>
    </fieldset>

}

但是当我发布...

    [HttpPost]
    public ActionResult FillForm(int id, FormFillModel model)
    {
        return RedirectToAction("UserPage");
    }

model.Fields 属性为空。

编辑:结果 Html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>FillForm</title>
    <link href="/Content/site.css" rel="stylesheet"/>

    <script src="/Scripts/modernizr-2.5.3.js"></script>

</head>
<body>
    <br />
    <a href="/Account/Logout">Logout</a>
    <br />


<h2>FillForm</h2>
<form action="/FormManage/FillForm/1" method="post">    <fieldset>
        <legend>TestForm2</legend>
        <input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Id" name="Id" type="hidden" value="1" />
        <table>
            <tr>
                <td>
                    <div class="display-label">
                        Title
                    </div>
                </td>
                <td>
                    <div class="display-field">
                        TestForm2
                    </div>
                </td>
            </tr>
            <tr>
                <td>
                    <div class="display-label">
                        Sector
                    </div>
                </td>
                <td>
                    <div class="display-field">

                    </div>
                </td>
            </tr>
            <tr>
                <td>
                    <div class="display-label">
                        Description
                    </div>
                </td>
                <td>
                    <div class="display-field">
                        TestForm Description
                    </div>
                </td>
            </tr>
        </table>
    </fieldset>
    <fieldset>
        <legend>Fields</legend>

        <div class="editor-label">
    <label for="FieldsList_Fields__0__Name">Name</label>
</div>
<div class="editor-field">
    <input id="FieldsList_Fields__0__Val" name="FieldsList.Fields.[0].Val" type="text" value="" />
</div>
<br /><div class="editor-label">
    <label for="FieldsList_Fields__1__Surname">Surname</label>
</div>
<div class="editor-field">
    <input id="FieldsList_Fields__1__Val" name="FieldsList.Fields.[1].Val" type="text" value="" />
</div>
<br /><div class="editor-label">
    <label for="FieldsList_Fields__2__Address">Address</label>
</div>
<div class="editor-field">
    <input id="FieldsList_Fields__2__Val" name="FieldsList.Fields.[2].Val" type="text" value="" />
</div>
<br />

        <p>
            <input type="submit" value="Submit" />
        </p>
    </fieldset>
</form>
<p>
    <a href="/FormManage/UserPage">Back</a>
</p>


    <script src="/Scripts/jquery-1.7.1.js"></script>


</body>
</html>

我应该怎么办?

4

1 回答 1

1

在迭代列表时尝试在编辑器模板中使用索引访问:

@for (int i=0; i<Model.Count(); i++)
{
    <div class="editor-field">
        @Html.TextBoxFor(model => Model[i].Val)
    </div>
}

这将使用输入字段的索引呈现名称,例如Fields[0].ValFields[1].Valmodelbinder 将值绑定到列表属性所需的索引。

编辑 我发现您的视图/模型不一致:

您的模型有一个名为Fields. 但是在您看来,您编写了模型。FieldsList .Fields`:

@Html.EditorFor( model => model.FieldsList.Fields, "FormFieldsListModel")

这怎么可能?您的视图的确切视图模型是什么?额外的FieldsList呈现错误的字段名,因此它无法在回发时绑定到模型的Fields属性。

于 2012-11-29T16:25:38.997 回答