I have an object called Job and one of the properties is a List of Steps:
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; }
}
I have a JobController with the following action to perform the update:
// PUT: /Job/Edit/5
[HttpPut]
public ActionResult Edit(Job model)
{
// Logic to update model here
}
Based on a the answer to this question I updated my UI (using the Bootstrap template that comes with MVC5) to:
@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>
}
As you can see I have to manually build the input tag opposed to using Html.EditorFor. The reason is that I need to control name of the id so that it passes the Index into the id and name. I would assume there is a better approach that would allow MVC to render the correct values using labelFor, EditorFor and ValidationMessageFor.
The questions I have are:
Option 1: Replace the foreach loop with 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>
}
Option 2: Create an editor template called Step.chtml for the Step class and use 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>
Main View
<h3>Steps</h3>
<div>
@Html.EditorFor(m => m.Steps)
<div>
In both these ways the framework will give the inputs correct names and ids.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With