Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- 1st is use razor to create dynamic js content
- html:
- @model IEnumerable<(Your model)>
- <table class="table table-striped table-hover">
- <thead class="" style="background-color:#0061A0;color:#fff;">
- <tr>
- <th>Nombre</th>
- <th>Distribucion actual (%)</th>
- <th style="width:200px;">Nueva distribucion (%)</th>
- </tr>
- </thead>
- <tbody>
- @foreach (var i in Model)
- {
- <tr>
- <td>@i.Nombre</td>
- <td>@i.Porcentaje</td>
- <td>@Html.TextBoxFor(x => i.NuevaDistribucion, new { @class = "form-control", Style = "width:75%;margin-left:25px;", @Name = "nuevaDistribucion" + iterador, id = iterador })</td>
- </tr>
- iterador++;
- }
- </tbody>
- </table>
- Js:
- @Scripts.Render("~/plugins/validate")
- @(iterador = 0);
- <script>
- $("#form1").removeAttr("novalidate");
- $(document).ready(function () {
- $("#form1").validate({
- rules: {
- @{
- for (var i = 0; i < Model.Count(); i++)
- {
- <text>
- @nuevaDistribucion@i: {
- required: true,
- min: 0,
- max: 100
- }@(i == Model.Count() ? "," : ",")
- </text>
- }
- }
- }
- });
- });
- </script>
- 2dn:
- html;
- @model IList<(Your model)>
- <table class="table table-striped table-hover">
- <thead class="" style="background-color:#0061A0;color:#fff;">
- <tr>
- <th>Nombre</th>
- <th>Distribucion actual (%)</th>
- <th style="width:200px;">Nueva distribucion (%)</th>
- </tr>
- </thead>
- <tbody>
- @for (var i = 0; i < Model.Count(); i++)
- {
- <tr>
- <td>@Model[i].Nombre</td>
- <td>@Model[i].Porcentaje</td>
- <td>@Html.TextBoxFor(x => Model[i].NuevaDistribucion, new { @class = "form-control", Style = "width:75%;margin-left:25px;", @Name = "nuevaDistribucion" + iterador, id = iterador })</td>
- </tr>
- iterador++;
- }
- </tbody>
- </table>
- js;
- @Scripts.Render("~/plugins/validate")
- <script>
- $("#form1").removeAttr("novalidate");
- $(document).ready(function () {
- $('#form1').validate();
- $('input[type="text"]').each(function () {
- $(this).rules('add', {
- required: true
- });
- });
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement