Advertisement
Guest User

FabricanteController

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