Advertisement
Guest User

EstadoController

a guest
Dec 18th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using ProyectoV1.Models;
  10.  
  11. namespace ProyectoV1.Controllers
  12. {
  13. public class EstadoController : Controller
  14. {
  15. private bdagricolaEntities db = new bdagricolaEntities();
  16.  
  17. // GET: Estado
  18. public ActionResult Index()
  19. {
  20. return View(db.estado.ToList());
  21. }
  22.  
  23. // GET: Estado/Details/5
  24. public ActionResult Details(int? id)
  25. {
  26. if (id == null)
  27. {
  28. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  29. }
  30. estado estado = db.estado.Find(id);
  31. if (estado == null)
  32. {
  33. return HttpNotFound();
  34. }
  35. return View(estado);
  36. }
  37.  
  38. // GET: Estado/Create
  39. public ActionResult Create()
  40. {
  41. return View();
  42. }
  43.  
  44. // POST: Estado/Create
  45. // Para protegerse de ataques de publicación excesiva, habilite las propiedades específicas a las que desea enlazarse. Para obtener
  46. // más información vea http://go.microsoft.com/fwlink/?LinkId=317598.
  47. [HttpPost]
  48. [ValidateAntiForgeryToken]
  49. public ActionResult Create([Bind(Include = "id,nombre")] estado estado)
  50. {
  51. if (ModelState.IsValid)
  52. {
  53. bool f = false;
  54. estado.nombre = estado.nombre.ToUpper();
  55. var estados = db.estado.Select(a => a.nombre);
  56. foreach (var a in estados)
  57. {
  58.  
  59. if (a == estado.nombre)
  60. {
  61. ViewBag.Error = "Estado ya Existe";
  62. f = true;
  63. }
  64.  
  65.  
  66. }
  67. if (f == true)
  68. {
  69. return View(estado);
  70. }
  71. else
  72. {
  73. if (ModelState.IsValid)
  74. {
  75. db.estado.Add(estado);
  76. db.SaveChanges();
  77. return RedirectToAction("Index");
  78. }
  79. }
  80.  
  81. }
  82.  
  83.  
  84. return View(estado);
  85. }
  86.  
  87. // GET: Estado/Edit/5
  88. public ActionResult Edit(int? id)
  89. {
  90. if (id == null)
  91. {
  92. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  93. }
  94. estado estado = db.estado.Find(id);
  95. if (estado == null)
  96. {
  97. return HttpNotFound();
  98. }
  99. return View(estado);
  100. }
  101.  
  102. // POST: Estado/Edit/5
  103. // Para protegerse de ataques de publicación excesiva, habilite las propiedades específicas a las que desea enlazarse. Para obtener
  104. // más información vea http://go.microsoft.com/fwlink/?LinkId=317598.
  105. [HttpPost]
  106. [ValidateAntiForgeryToken]
  107. public ActionResult Edit([Bind(Include = "id,nombre")] estado estado)
  108. {
  109. bool f = false;
  110. estado.nombre = estado.nombre.ToUpperInvariant();
  111. var estados = db.estado.Select(a => a.nombre);
  112. foreach (var a in estados)
  113. {
  114.  
  115. if (a == estado.nombre)
  116. {
  117. ViewBag.Error = "Estado ya Existe";
  118. f = true;
  119. }
  120.  
  121.  
  122. }
  123. if (f == true)
  124. {
  125. return View(estado);
  126. }
  127. else
  128. {
  129. if (ModelState.IsValid)
  130. {
  131. db.Entry(estado).State = EntityState.Modified;
  132. db.SaveChanges();
  133. return RedirectToAction("Index");
  134. }
  135. }
  136.  
  137. return View(estado);
  138. }
  139.  
  140. // GET: Estado/Delete/5
  141. public ActionResult Delete(int? id)
  142. {
  143. if (id == null)
  144. {
  145. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  146. }
  147. estado estado = db.estado.Find(id);
  148. if (estado == null)
  149. {
  150. return HttpNotFound();
  151. }
  152. return View(estado);
  153. }
  154.  
  155. // POST: Estado/Delete/5
  156. [HttpPost, ActionName("Delete")]
  157. [ValidateAntiForgeryToken]
  158. public ActionResult DeleteConfirmed(int id)
  159. {
  160. estado estado = db.estado.Find(id);
  161. try
  162. {
  163.  
  164. db.estado.Remove(estado);
  165. db.SaveChanges();
  166. return RedirectToAction("Index");
  167. }
  168. catch
  169. {
  170. ViewBag.Error = "No se puede eliminar debido a que existen datos asociados";
  171. }
  172. return View(estado);
  173. }
  174.  
  175. protected override void Dispose(bool disposing)
  176. {
  177. if (disposing)
  178. {
  179. db.Dispose();
  180. }
  181. base.Dispose(disposing);
  182. }
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement