【发布时间】:2016-12-07 07:18:02
【问题描述】:
我有一个名为 Job 的对象,其中一个属性是一个步骤列表:
public class Job
{
[Display(Name = "Id")]
public int? JobId { get; set; }
[Required]
public string Name { get; set; }
public List<Step> Steps { get; set; }
public Job()
{
Steps = new List<Step>();
}
}
public class Step
{
public int? StepId { get; set; }
public int JobId { get; set; }
[Required]
public string Name { get; set; }
}
我有一个带有以下操作的 JobController 来执行更新:
// PUT: /Job/Edit/5
[HttpPut]
public ActionResult Edit(Job model)
{
// Logic to update model here
}
根据question 的答案,我将我的 UI(使用 MVC5 附带的 Bootstrap 模板)更新为:
@using (Html.BeginForm())
{
@Html.HttpMethodOverride(HttpVerbs.Put)
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.JobId)
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<h3>Steps</h3>
<div>
@foreach (var item in Model.Steps)
{
<div class="form-group">
@Html.Hidden("Steps[" + stepIndex + "].StepId", item.StepId)
@Html.LabelFor(modelItem => item.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input class="form-control text-box single-line" data-val="true" data-val-required="The Name field is required."
id="@String.Format("Steps_{0}__Name", stepIndex)" name="@String.Format("Steps[{0}].Name", stepIndex)" type="text" value="@item.Name" />
@Html.ValidationMessageFor(modelItem => item.Name, "", new { @class = "text-danger" })
</div>
</div>
stepIndex += 1;
<hr />
}
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
如您所见,我必须手动构建输入标签,而不是使用 Html.EditorFor。原因是我需要控制 id 的名称,以便它将索引传递给 id 和名称。我假设有一种更好的方法可以让 MVC 使用 labelFor、EditorFor 和 ValidationMessageFor 呈现正确的值。
我的问题是:
- 是否有一组可用于 MVC5 的控件,使我无需经过这些额外步骤即可渲染复杂的子对象?
- 如果不是 1,那么有没有比手动创建输入标签更好的方法?
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-5