Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. When you use a list and iterate over there, Razor render engine create an specific name for every element in list, so we have 2 solutions.
  2.  
  3. 1st is use razor to create dynamic js content
  4. html:
  5. @model IEnumerable<(Your model)>
  6.  
  7. <table class="table table-striped table-hover">
  8. <thead class="" style="background-color:#0061A0;color:#fff;">
  9. <tr>
  10. <th>Nombre</th>
  11. <th>Distribucion actual (%)</th>
  12. <th style="width:200px;">Nueva distribucion (%)</th>
  13. </tr>
  14. </thead>
  15. <tbody>
  16.  
  17. @foreach (var i in Model)
  18. {
  19.  
  20. <tr>
  21. <td>@i.Nombre</td>
  22. <td>@i.Porcentaje</td>
  23. <td>@Html.TextBoxFor(x => i.NuevaDistribucion, new { @class = "form-control", Style = "width:75%;margin-left:25px;", @Name = "nuevaDistribucion" + iterador, id = iterador })</td>
  24. </tr>
  25. iterador++;
  26. }
  27.  
  28. </tbody>
  29. </table>
  30.  
  31. Js:
  32. @Scripts.Render("~/plugins/validate")
  33. @(iterador = 0);
  34. <script>
  35. $("#form1").removeAttr("novalidate");
  36. $(document).ready(function () {
  37. $("#form1").validate({
  38. rules: {
  39. @{
  40. for (var i = 0; i < Model.Count(); i++)
  41. {
  42. <text>
  43. @nuevaDistribucion@i: {
  44. required: true,
  45. min: 0,
  46. max: 100
  47. }@(i == Model.Count() ? "," : ",")
  48. </text>
  49.  
  50. }
  51.  
  52. }
  53. }
  54. });
  55. });
  56.  
  57. </script>
  58.  
  59. 2dn:
  60.  
  61. html;
  62.  
  63. @model IList<(Your model)>
  64.  
  65. <table class="table table-striped table-hover">
  66. <thead class="" style="background-color:#0061A0;color:#fff;">
  67. <tr>
  68. <th>Nombre</th>
  69. <th>Distribucion actual (%)</th>
  70. <th style="width:200px;">Nueva distribucion (%)</th>
  71. </tr>
  72. </thead>
  73. <tbody>
  74.  
  75. @for (var i = 0; i < Model.Count(); i++)
  76. {
  77.  
  78. <tr>
  79. <td>@Model[i].Nombre</td>
  80. <td>@Model[i].Porcentaje</td>
  81. <td>@Html.TextBoxFor(x => Model[i].NuevaDistribucion, new { @class = "form-control", Style = "width:75%;margin-left:25px;", @Name = "nuevaDistribucion" + iterador, id = iterador })</td>
  82. </tr>
  83. iterador++;
  84. }
  85.  
  86. </tbody>
  87. </table>
  88.  
  89. js;
  90.  
  91. @Scripts.Render("~/plugins/validate")
  92. <script>
  93.  
  94. $("#form1").removeAttr("novalidate");
  95. $(document).ready(function () {
  96.  
  97. $('#form1').validate();
  98. $('input[type="text"]').each(function () {
  99. $(this).rules('add', {
  100. required: true
  101. });
  102. });
  103.  
  104. });
  105.  
  106. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement