这是一个非常普遍的问题,但我被卡住了!
我有一个模型类,我想在视图中编辑它:
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<SoruCevapForm.Models.FormFieldModel>
@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>
我应该怎么办?