Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. public class ViewModelA {
  2. public ViewModelB CurrentItem { get; set; }
  3. // ...
  4. }
  5.  
  6. public class ViewModelB {
  7. public List<Parameter> Parameters { get; set; }
  8. // ...
  9. }
  10.  
  11. public class Parameter {
  12. public string Name { get; set; }
  13. public int Position { get; set; }
  14. // ...
  15. }
  16.  
  17. @model ViewModelA
  18.  
  19. @* ... *@
  20.  
  21. @for (int i = 0; i < Model.CurrentItem.Parameters.Count; i++) {
  22. @Html.EditorFor(m => m.CurrentItem.Parameters[i])
  23. }
  24.  
  25. @model Parameter
  26.  
  27. <tr>
  28. <td class="col-md-3">
  29. </td>
  30. <td class="col-md-9">
  31. @Html.TextBoxFor(m => m.Position)
  32. @Html.TextBoxFor(m => m.Name)
  33.  
  34. @* Model.Position always equals list index + 1 *@
  35. <button type="submit" class="btn btn-default" formaction="@Url.Action("DeleteParameter", "MyController", new { deleteIndex = (Model.Position - 1) })" for formmethod="post" title="Delete parameter">
  36. <i class="glyphicon glyphicon-minus"></i>
  37. </button>
  38.  
  39. <!-- Debug only -->
  40. <span>Position = @Model.Position | Name = @Model.Name</span>
  41. </td>
  42. </tr>
  43.  
  44. [HttpPost]
  45. public ActionResult AddParameter(ViewModelA viewModel) {
  46. var p = new Parameter() {
  47. Name = "",
  48. Position = (byte)(viewModel.CurrentItem.Parameters.Count + 1)
  49. //...
  50. };
  51.  
  52. viewModel.CurrentItem.Parameters.Add(p);
  53.  
  54. return View("Edit", viewModel);
  55. }
  56.  
  57. [HttpPost]
  58. public ActionResult DeleteParameter(int deleteIndex, ViewModelA viewModel) {
  59. viewModel.CurrentItem.Parameters.RemoveAt(deleteIndex);
  60.  
  61. for (int i = deleteIndex; i < viewModel.CurrentItem.Parameters.Count; i++) {
  62. viewModel.CurrentItem.Parameters[i].Position--;
  63. }
  64.  
  65. return View("Edit", viewModel);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement