Advertisement
DimasGarcia

Form.cshtml

May 22nd, 2021
2,132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASP 4.98 KB | None | 0 0
  1. @model ADSProjectLab.Models.Grupo
  2. @{
  3.     ViewBag.Title = "Form";
  4.     Layout = "~/Views/Shared/_Layout.cshtml";
  5. }
  6.  
  7. <h2>Asignación de grupo</h2>
  8. <hr />
  9.  
  10. @using (Html.BeginForm("Form", "AsignacionGrupo", FormMethod.Post))
  11. {
  12.     @Html.AntiForgeryToken()
  13.     <input type="hidden" id="IdGrupo" value="@Model.Id" name="Id" />
  14.     <input type="hidden" id="_redirectURL" value='@Url.Action("Index", "Grupos")' />
  15.     <div class="form-horizontal">
  16.         <div class="row">
  17.             <div class="col-md-2">
  18.                 <label>Año:</label>
  19.                 @Html.DisplayFor(m => m.Anio)
  20.             </div>
  21.             <div class="col-md-2">
  22.                 <label>Ciclo:</label>
  23.                 @Html.DisplayFor(m => m.Ciclo)
  24.             </div>
  25.             <div class="col-md-4">
  26.                 <label>Profesor:</label>
  27.                 <span>@(Model.Profesor.Nombres + " " + Model.Profesor.Apellidos)</span>
  28.             </div>
  29.             <div class="col-md-4">
  30.                 <label>Materia:</label>
  31.                 @Html.DisplayFor(m => m.Materia.Nombre)
  32.             </div>
  33.         </div>
  34.         <br />
  35.         <div class="row">
  36.             <div class="col-md-4">
  37.                 <label>Carrera:</label>
  38.                 @Html.DisplayFor(m => m.Carrera.Nombre)
  39.             </div>
  40.         </div>
  41.         <br />
  42.         <div class="row">
  43.             <div class="col-md-1">
  44.                 <label for="IdEstudiante" class="control-label">Estudiante:</label>
  45.             </div>
  46.             <div class="col-md-3">
  47.                 @Html.DropDownList("IdEstudiante", new SelectList(ViewBag.Estudiantes, "Id", "Nombres"), "[Seleccione una opción]", htmlAttributes: new { @class = "form-control" })
  48.             </div>
  49.             <div class="col-md-2">
  50.                 <button type="button" value="Agregar" class="btn btn-default" onclick="agregarFila()">Agregar</button>
  51.             </div>
  52.         </div>
  53.         <hr />
  54.         <table class="table table-bordered table-striped">
  55.             <thead>
  56.                 <tr>
  57.                     <th>
  58.                         Nombre completo del Estudiante
  59.                     </th>
  60.                     <th>Acción</th>
  61.                 </tr>
  62.             </thead>
  63.             <tbody>
  64.                 @{
  65.                     int i = 0;
  66.                 }
  67.                 @foreach (var item in Model.AsignacionGrupos)
  68.                 {
  69.                     <tr>
  70.                         <td>
  71.                             <span>@(item.Estudiante.Nombres + " " + item.Estudiante.Apellidos)</span>
  72.                             <input type="hidden" name="AsignacionGrupos[@i].IdEstudiante" value="@item.Estudiante.Id" class="IdEstudiante" />
  73.                             <input type="hidden" name="AsignacionGrupos[@i].IdGrupo" value="@Model.Id" class="IdGrupo" />
  74.                         </td>
  75.                         <td>
  76.                             <button class="btn btn-xs btn-danger" onclick="quitarFila(this)">Eliminar</button>
  77.                         </td>
  78.                     </tr>
  79.                     {
  80.                         i++;
  81.                     }
  82.                 }
  83.             </tbody>
  84.         </table>
  85.         <div class="row">
  86.             <div class="col-md-2">
  87.                 <input type="submit" value="Guardar" class="btn btn-default" />
  88.             </div>
  89.         </div>
  90.  
  91.     </div>
  92. }
  93. <br />
  94. <br />
  95. <div>
  96.     @Html.ActionLink("Regresar", "Index", "Grupos", new { }, new { @class = "btn btn-default btn-sm" })
  97. </div>
  98.  
  99. <style>
  100.     .table td:last-child, th:last-child {
  101.         text-align: center;
  102.     }
  103. </style>
  104. @section Scripts {
  105.     @Scripts.Render("~/bundles/jqueryval")
  106.     <script>
  107.         function quitarFila(elemento) {
  108.             $(elemento).parent().closest("tr").remove();
  109.             recalcularIndices();
  110.         }
  111.         function agregarFila() {
  112.             var fila = $("<tr><td><span>" + $("#IdEstudiante option:selected").text() + "</span><input type='hidden' value='" + $("#IdEstudiante").val() + "' class='IdEstudiante' /><input type='hidden' value='" + $("#IdGrupo").val() + "' class='IdGrupo' /></td><td><button class='btn btn-xs btn-danger' onclick='quitarFila(this)'>Eliminar</button></td></tr>");
  113.             $("tbody").append(fila);
  114.             recalcularIndices();
  115.         }
  116.         function recalcularIndices() {
  117.             var i = 0;
  118.             $(".IdEstudiante").each(function () {
  119.                 $(this).attr("name", "AsignacionGrupos[" + i + "].IdEstudiante");
  120.                 i++;
  121.             });
  122.             i = 0;
  123.             $(".IdGrupo").each(function () {
  124.                 $(this).attr("name", "AsignacionGrupos[" + i + "].IdGrupo");
  125.                 i++;
  126.             });
  127.         }
  128.         $(document).ready(function () {
  129.             //Agregamos a los formularios la clase form-ajax
  130.             //La cual activa el formulario para trabajar con Ajax
  131.             $("form").addClass("form-ajax");
  132.             //Activamos Ajax
  133.             initAjaxForm();
  134.         });
  135.     </script>
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement