ASP.NET MVC 5复杂列表子项的编辑选项

Jos*_*osh 3 c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-5

我有一个名为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; }
}
Run Code Online (Sandbox Code Playgroud)

我有一个JobController,它具有以下操作来执行更新:

   // PUT: /Job/Edit/5
   [HttpPut]
   public ActionResult Edit(Job model)
   {
     // Logic to update model here
   }
Run Code Online (Sandbox Code Playgroud)

根据这个问题的答案,我更新了我的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>
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我必须手动构建输入标记,而不是使用Html.EditorFor.原因是我需要控制id的名称,以便将索引传递给id和name.我认为有一种更好的方法可以让MVC使用labelFor,EditorFor和ValidationMessageFor呈现正确的值.

我的问题是:

  1. 是否有一组控件可以与MVC5一起使用,它允许我渲染复杂的子对象而无需经过这些额外的步骤?
  2. 如果没有1,那么有没有比手动创建输入标签更好的方法?

Zab*_*sky 7

选项1:用以下代码替换foreach循环for:

@for (int i = 0; i < Model.Steps.Count; i++)
{
    <div class="form-group">
        @Html.HiddenFor(m => m.Steps[i].StepId)
        @Html.TextBoxFor(m => m.Steps[i].Name, new { @class = "form-control text-box single-line" })
        ...
    </div>
}
Run Code Online (Sandbox Code Playgroud)

选项2:创建一个Step.chtml为Step该类调用的编辑器模板并使用EditorFor:

Step.chtml

@model Step
<div class="form-group">
    @Html.HiddenFor(m => m.StepId)
    @Html.TextBoxFor(m => m.Name, new { @class = "form-control text-box single-line" })
    ...
</div>
Run Code Online (Sandbox Code Playgroud)

主视图

<h3>Steps</h3>
<div>
    @Html.EditorFor(m => m.Steps)
<div>
Run Code Online (Sandbox Code Playgroud)

在这两种方式中,框架将为输入提供正确的名称和ID.