これは非常に一般的な問題ですが、行き詰まりました。
モデルクラスがあり、ビューで編集したい:
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; }
}
文字列プロパティは編集できますが、Fields プロパティは編集できません。
そのためのエディター テンプレートを作成しました。~/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>
私は何をすべきか?