Advertisement
DimasGarcia

Form.cshtml

May 22nd, 2021
2,130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASP 5.50 KB | None | 0 0
  1. @model ADSProjectLab.Models.Grupo
  2.  
  3. @{
  4.     ViewBag.Title = "Form";
  5.     Layout = "~/Views/Shared/_Layout.cshtml";
  6. }
  7.  
  8. <h2>Grupo</h2>
  9.  
  10.  
  11. @using (Html.BeginForm())
  12. {
  13.     @Html.AntiForgeryToken()
  14.     <input type="hidden" id="_operacion" value="@ViewData["Operacion"]" />
  15.     <input type="hidden" id="_redirectURL" value='@Url.Action("Index", "Grupos")' />
  16.     <div class="form-horizontal">
  17.         <hr />
  18.         @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  19.         @Html.HiddenFor(model => model.Id)
  20.  
  21.         <div class="form-group">
  22.             @Html.LabelFor(model => model.IdCarrera, htmlAttributes: new { @class = "control-label col-md-2" })
  23.             <div class="col-md-10">
  24.                 @Html.DropDownListFor(model => model.IdCarrera, new SelectList(ViewBag.Carreras, "Id", "Nombre"), "[Seleccione una opción]", htmlAttributes: new { @class = "form-control" })
  25.                 @Html.ValidationMessageFor(model => model.IdCarrera, "", new { @class = "text-danger" })
  26.             </div>
  27.         </div>
  28.  
  29.         <div class="form-group">
  30.             @Html.LabelFor(model => model.IdMateria, htmlAttributes: new { @class = "control-label col-md-2" })
  31.             <div class="col-md-10">
  32.                 @Html.DropDownListFor(model => model.IdMateria, new SelectList(ViewBag.Materias, "Id", "Nombre"), "[Seleccione una opción]", htmlAttributes: new { @class = "form-control" })
  33.                 @Html.ValidationMessageFor(model => model.IdMateria, "", new { @class = "text-danger" })
  34.             </div>
  35.         </div>
  36.  
  37.         <div class="form-group">
  38.             @Html.LabelFor(model => model.IdProfesor, htmlAttributes: new { @class = "control-label col-md-2" })
  39.             <div class="col-md-10">
  40.                 @Html.DropDownListFor(model => model.IdProfesor, new SelectList(ViewBag.Profesores, "Id", "Nombres"), "[Seleccione una opción]", htmlAttributes: new { @class = "form-control" })
  41.                 @Html.ValidationMessageFor(model => model.IdProfesor, "", new { @class = "text-danger" })
  42.             </div>
  43.         </div>
  44.  
  45.         <div class="form-group">
  46.             @Html.LabelFor(model => model.Ciclo, htmlAttributes: new { @class = "control-label col-md-2" })
  47.             <div class="col-md-10">
  48.                 @Html.EditorFor(model => model.Ciclo, new { htmlAttributes = new { @class = "form-control" } })
  49.                 @Html.ValidationMessageFor(model => model.Ciclo, "", new { @class = "text-danger" })
  50.             </div>
  51.         </div>
  52.  
  53.         <div class="form-group">
  54.             @Html.LabelFor(model => model.Anio, htmlAttributes: new { @class = "control-label col-md-2" })
  55.             <div class="col-md-10">
  56.                 @Html.EditorFor(model => model.Anio, new { htmlAttributes = new { @class = "form-control" } })
  57.                 @Html.ValidationMessageFor(model => model.Anio, "", new { @class = "text-danger" })
  58.             </div>
  59.         </div>
  60.  
  61.         <div class="form-group">
  62.             <div class="col-md-offset-2 col-md-10">
  63.                 <input type="submit" value="Guardar" class="btn btn-default" />
  64.             </div>
  65.         </div>
  66.     </div>
  67. }
  68.  
  69. <div>
  70.     @Html.ActionLink("Regresar", "Index", new { }, new { @class = "btn btn-default btn-sm" })
  71. </div>
  72.  
  73. @section Scripts {
  74.     @Scripts.Render("~/bundles/jqueryval")
  75.     <script>
  76.  
  77.         $(document).ready(function () {
  78.             // Cuando la operación es Ver, entonces se deben desabilitar los controles del formulario.
  79.             if ($("#_operacion").val() == "Ver") {
  80.                 $("form").find("input, select").attr("disabled", true);
  81.             }
  82.             //Agregamos a los formularios la clase form-ajax
  83.             //La cual activa el formulario para trabajar con Ajax
  84.             $("form").addClass("form-ajax");
  85.             //Activamos Ajax
  86.             initAjaxForm();
  87.  
  88.             $("#IdCarrera").change(cargarMaterias);
  89.  
  90.             $("#IdCarrera").trigger("change");
  91.         });
  92.  
  93.         function cargarMaterias() {
  94.             cargarDataCombobox('@Url.Action("CargarMaterias", "Grupos")?IdCarrera=' + $("#IdCarrera").val(), $("#IdMateria"));
  95.         }
  96.  
  97.         function cargarDataCombobox(URL, cmb) {
  98.             $.ajax({
  99.                 url: URL,
  100.                 headers: {
  101.                     'Accept': "application/json; charset=utf-8"
  102.                 },
  103.                 beforeSend: function () {
  104.                     console.log("Iniciando petición...");
  105.                 },
  106.                 success: function (data) {
  107.                     var valorSeleccionado = $("#IdMateria").val();
  108.                     cmb.html("<option value=''>[Seleccione una opción]</option>");
  109.                     $.each(data, function (index, item) {
  110.                         if (valorSeleccionado == item.Id) {
  111.                             cmb.append(`<option selected value=${item.Id}>${item.Nombre}</option>`);
  112.                         } else {
  113.                             cmb.append(`<option value=${item.Id}>${item.Nombre}</option>`);
  114.                         }
  115.                     });
  116.                 },
  117.                 error: function (xhr, textStatus, errorThrown) {
  118.                     console.log("Ocurrió un error en la petición.");
  119.                 },
  120.                 complete: function () {
  121.                     console.log("Petición completada.");
  122.                 },
  123.                 type: "GET",
  124.                 contentType: false,
  125.                 processData: false,
  126.                 async: true,
  127.                 cache: false
  128.             });
  129.         }
  130.  
  131.     </script>
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement