Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. public ActionResult CrearCliente()
  2. {
  3. return View(new ClsCliente());
  4. }
  5.  
  6. [HttpPost]
  7. public ActionResult CrearCliente(ClsCliente cliente)
  8. {
  9.  
  10. if (!ModelState.IsValid)
  11. {
  12. ViewBag.mensaje = "error";
  13. return View(cliente);
  14. }
  15.  
  16. string mensaje = "";
  17.  
  18. try
  19. {
  20. SqlCommand cmd = new SqlCommand("insert into tb_clientes values(@id,@nombre,@direc,@idpais,@fono)", con);
  21. cmd.Parameters.AddWithValue("@id", cliente.idcliente);
  22. cmd.Parameters.AddWithValue("@nombre", cliente.nommbrecia);
  23. cmd.Parameters.AddWithValue("@direc", cliente.direccion);
  24. cmd.Parameters.AddWithValue("@idpais", cliente.idpais);
  25. cmd.Parameters.AddWithValue("@fono", cliente.telefono);
  26. con.Open();
  27. cmd.ExecuteNonQuery();
  28.  
  29. mensaje = "exito";
  30. }
  31. catch (SqlException ex)
  32. {
  33. mensaje = ex.Message;
  34. }
  35. finally
  36. {
  37. con.Close();
  38. }
  39.  
  40. ViewBag.mensaje = mensaje;
  41.  
  42.  
  43.  
  44. return RedirectToAction("MostrarClientes");
  45. }
  46.  
  47. @model FormularioCharlas.Models.ClsCliente
  48.  
  49. @{
  50. ViewBag.Title = "CrearCliente";
  51. Layout = "~/Views/Shared/_Layout.cshtml";
  52. }
  53.  
  54. <h2>CrearCliente</h2>
  55.  
  56.  
  57. @using (Html.BeginForm())
  58. {
  59. @Html.AntiForgeryToken()
  60.  
  61. <div class="form-horizontal">
  62. <h4>ClsCliente</h4>
  63. <hr />
  64. @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  65. <div class="form-group">
  66. @Html.LabelFor(model => model.idcliente, htmlAttributes: new { @class = "control-label col-md-2" })
  67. <div class="col-md-10">
  68. @Html.EditorFor(model => model.idcliente, new { htmlAttributes = new { @class = "form-control" } })
  69. @Html.ValidationMessageFor(model => model.idcliente, "", new { @class = "text-danger" })
  70. </div>
  71. </div>
  72.  
  73. <div class="form-group">
  74. @Html.LabelFor(model => model.nommbrecia, htmlAttributes: new { @class = "control-label col-md-2" })
  75. <div class="col-md-10">
  76. @Html.EditorFor(model => model.nommbrecia, new { htmlAttributes = new { @class = "form-control" } })
  77. @Html.ValidationMessageFor(model => model.nommbrecia, "", new { @class = "text-danger" })
  78. </div>
  79. </div>
  80.  
  81. <div class="form-group">
  82. @Html.LabelFor(model => model.direccion, htmlAttributes: new { @class = "control-label col-md-2" })
  83. <div class="col-md-10">
  84. @Html.EditorFor(model => model.direccion, new { htmlAttributes = new { @class = "form-control" } })
  85. @Html.ValidationMessageFor(model => model.direccion, "", new { @class = "text-danger" })
  86. </div>
  87. </div>
  88.  
  89. <div class="form-group">
  90. @Html.LabelFor(model => model.idpais, htmlAttributes: new { @class = "control-label col-md-2" })
  91. <div class="col-md-10">
  92. @Html.EditorFor(model => model.idpais, new { htmlAttributes = new { @class = "form-control" } })
  93. @Html.ValidationMessageFor(model => model.idpais, "", new { @class = "text-danger" })
  94. </div>
  95. </div>
  96.  
  97. <div class="form-group">
  98. @Html.LabelFor(model => model.telefono, htmlAttributes: new { @class = "control-label col-md-2" })
  99. <div class="col-md-10">
  100. @Html.EditorFor(model => model.telefono, new { htmlAttributes = new { @class = "form-control" } })
  101. @Html.ValidationMessageFor(model => model.telefono, "", new { @class = "text-danger" })
  102. </div>
  103. </div>
  104.  
  105. <div class="form-group">
  106. <div class="col-md-offset-2 col-md-10">
  107. <input type="submit" value="Create" id="id_cliente" class="btn btn-default" />
  108. </div>
  109. </div>
  110. </div>
  111. }
  112.  
  113. <div>
  114. @Html.ActionLink("Back to List", "Index")
  115. </div>
  116.  
  117. @section Scripts {
  118. @Scripts.Render("~/bundles/jqueryval")
  119. }
  120.  
  121. <script type="text/javascript">
  122. //===============SWEET ALERT===============
  123.  
  124. var msg = "@ViewBag.mensaje";
  125.  
  126. if (msg != "") {
  127. if (msg == "exito") {
  128. Swal.fire(
  129. 'Registro Exitoso!',
  130. 'Se agrego una nueva charla!',
  131. 'success'
  132. );
  133.  
  134. //alert(msg);
  135. } else {
  136. Swal.fire(
  137. 'Ocurrio un error!',
  138. 'No se pudo registrar su nueva charla, asegúrese de completar todo el formulario, si el problema continua comuníquese con el área de informática!',
  139. 'error'
  140. );
  141. }
  142. }
  143. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement