Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ViewModelA {
- public ViewModelB CurrentItem { get; set; }
- // ...
- }
- public class ViewModelB {
- public List<Parameter> Parameters { get; set; }
- // ...
- }
- public class Parameter {
- public string Name { get; set; }
- public int Position { get; set; }
- // ...
- }
- @model ViewModelA
- @* ... *@
- @for (int i = 0; i < Model.CurrentItem.Parameters.Count; i++) {
- @Html.EditorFor(m => m.CurrentItem.Parameters[i])
- }
- @model Parameter
- <tr>
- <td class="col-md-3">
- </td>
- <td class="col-md-9">
- @Html.TextBoxFor(m => m.Position)
- @Html.TextBoxFor(m => m.Name)
- @* Model.Position always equals list index + 1 *@
- <button type="submit" class="btn btn-default" formaction="@Url.Action("DeleteParameter", "MyController", new { deleteIndex = (Model.Position - 1) })" for formmethod="post" title="Delete parameter">
- <i class="glyphicon glyphicon-minus"></i>
- </button>
- <!-- Debug only -->
- <span>Position = @Model.Position | Name = @Model.Name</span>
- </td>
- </tr>
- [HttpPost]
- public ActionResult AddParameter(ViewModelA viewModel) {
- var p = new Parameter() {
- Name = "",
- Position = (byte)(viewModel.CurrentItem.Parameters.Count + 1)
- //...
- };
- viewModel.CurrentItem.Parameters.Add(p);
- return View("Edit", viewModel);
- }
- [HttpPost]
- public ActionResult DeleteParameter(int deleteIndex, ViewModelA viewModel) {
- viewModel.CurrentItem.Parameters.RemoveAt(deleteIndex);
- for (int i = deleteIndex; i < viewModel.CurrentItem.Parameters.Count; i++) {
- viewModel.CurrentItem.Parameters[i].Position--;
- }
- return View("Edit", viewModel);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement