Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 自定义类的模板_Asp.net Mvc 4_C# 4.0 - Fatal编程技术网

Asp.net mvc 4 自定义类的模板

Asp.net mvc 4 自定义类的模板,asp.net-mvc-4,c#-4.0,Asp.net Mvc 4,C# 4.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<FormFieldM

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

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

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

填充


填充 测试格式2 标题 测试格式2 部门 描述 测试表单描述 领域 名称

地址


我该怎么办?

在迭代列表时,尝试在editortemplate中使用索引访问:

@for (int i=0; i<Model.Count(); i++)
{
    <div class="editor-field">
        @Html.TextBoxFor(model => Model[i].Val)
    </div>
}
这怎么可能?您的视图的确切视图模型是什么?
附加的字段列表呈现了错误的字段名,因此它无法绑定回发帖时模型的字段属性。

但我没有使用DefaultModelBinder。有趣的是,对于HttpGet,编辑器模板正在工作。我的意思是,当我显示表单时,这是有效的。但是当我发帖时,模型对象返回的属性为空。@piedpiper我想你正在使用它-但是,我还没有把你的问题读到最后。我已经完全改写了我的答案。请再次检查。请发布为字段propertyOk的输入元素之一生成的HTML,我编辑了我的帖子。请检查。我怀疑出了什么问题,但我找不到解决方案……我知道这看起来很可笑,但FormFieldsListModel.cshtml接受与model.FieldsList.Fields类型相同的列表。
    [HttpPost]
    public ActionResult FillForm(int id, FormFillModel model)
    {
        return RedirectToAction("UserPage");
    }
<!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>
@for (int i=0; i<Model.Count(); i++)
{
    <div class="editor-field">
        @Html.TextBoxFor(model => Model[i].Val)
    </div>
}
@Html.EditorFor( model => model.FieldsList.Fields, "FormFieldsListModel")