En la parte primera de esta entrada, nos enfocamos en las vistas [Listado y Ver]
Las vistas Crear y Editar lucen como sigue (son muy similares, lo único que cambia que Editar lleva el Código en sólo lectura):
ProductoController
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Business; using Entity; using System.Data; namespace Web.Controllers { public class ProductoController : Controller { private ProductoBusiness _business; private ProveedorBusiness prov; private CategoriaBusiness cate; public ProductoController() { _business = new ProductoBusiness(); prov = new ProveedorBusiness(); cate = new CategoriaBusiness(); } [HttpGet] public ActionResult Crear() { ViewBag.ListaProveedores = new SelectList(prov.GetAll(), "Id", "Nombre", "Nombre"); ViewBag.ListaCategorias = new SelectList(cate.GetAll(), "Id", "Nombre", "Nombre"); ViewBag.ListaEstados = Utilities.Utilities.GetEstados(); return View(); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Crear(Producto p) { try { if (ModelState.IsValid) { ViewBag.ListaProveedores = new SelectList(prov.GetAll(), "Id", "Nombre", "Nombre"); ViewBag.ListaCategorias = new SelectList(cate.GetAll(), "Id", "Nombre", "Nombre"); ViewBag.ListaEstados = Utilities.Utilities.GetEstados(); if (_business.Insert(p)) { WriteMessage(Eoperacion.insert, Etipo.success); //ModelState.Clear(); } } return View(); } catch (Exception ex) { ModelState.AddModelError("", "Error al registrar el producto - " + ex.Message); WriteMessage(Eoperacion.insert, Etipo.danger); return View(); } } [HttpGet] public ActionResult Editar(int id) { ViewBag.ListaProveedores = new SelectList(prov.GetAll(), "Id", "Nombre", "Nombre"); ViewBag.ListaCategorias = new SelectList(cate.GetAll(), "Id", "Nombre", "Nombre"); ViewBag.ListaEstados = Utilities.Utilities.GetEstados(); Producto p = new Producto(); p = _business.GetById(id); return View(p); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Editar(int id, Producto p) { try { if (ModelState.IsValid) { ViewBag.ListaProveedores = new SelectList(prov.GetAll(), "Id", "Nombre", "Nombre"); ViewBag.ListaCategorias = new SelectList(cate.GetAll(), "Id", "Nombre", "Nombre"); ViewBag.ListaEstados = Utilities.Utilities.GetEstados(); if (_business.Update(p)) { WriteMessage(Eoperacion.update, Etipo.success); } else { WriteMessage(Eoperacion.update, Etipo.danger); } ModelState.Clear(); } } catch (DataException) { ModelState.AddModelError("", "Unable to edit employee record."); WriteMessage(Eoperacion.update, Etipo.danger); } return View(p); } #region Metodos Auxiliares public enum Eoperacion : byte { insert = 1, update = 2, delete = 3, find = 4, reset = 5 } public enum Etipo : byte { success = 1, danger = 2, info = 3 } public void WriteMessage(Eoperacion Operacion, Etipo Tipo) { if (Tipo == Etipo.success) { switch (Operacion) { case Eoperacion.insert: ViewBag.MensajeExito = "El producto ha sido correctamente creado."; break; case Eoperacion.update: ViewBag.MensajeExito = "El producto ha sido correctamente editado."; break; case Eoperacion.delete: ViewBag.MensajeExito = "El producto ha sido correctamente eliminado."; break; } } else if(Tipo == Etipo.danger) { switch (Operacion) { case Eoperacion.insert: ViewBag.MensajeError = "No se ha podido crear el producto."; break; case Eoperacion.update: ViewBag.MensajeError = "No se ha podido editar el producto"; break; case Eoperacion.delete: ViewBag.MensajeError = "No se ha podido eliminar el producto"; break; } } else { switch (Operacion) { case Eoperacion.find: ViewBag.MensajeBusqueda = "No se encontraron resultados para su búsqueda."; break; case Eoperacion.reset: ViewBag.MensajeBusqueda = ""; break; } } } #endregion } }Vista Crear Producto
@model Entity.Producto @{ ViewBag.Title = "Crear Producto"; }Vista Editar ProductoCrear Nuevo Producto
@using (Html.BeginForm()) { @Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })}
@Html.LabelFor(model => model.Nombre, htmlAttributes: new { @class = "col-sm-3 col-md-4 col-lg-2 col-form-label font-weight-bold text-md-right" })
@Html.EditorFor(model => model.Nombre, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Nombre, "", new { @class = "text-danger" })@Html.LabelFor(model => model.Descripcion, htmlAttributes: new { @class = "col-sm-3 col-md-4 col-lg-2 col-form-label font-weight-bold text-md-right" })
@Html.TextAreaFor(model => model.Descripcion, new { @class = "form-control", @rows = "3", @cols = "50" }) @Html.ValidationMessageFor(model => model.Descripcion, "", new { @class = "text-danger" })@Html.LabelFor(model => model.PrecioUnitario, htmlAttributes: new { @class = "col-sm-3 col-md-4 col-lg-2 col-form-label font-weight-bold text-md-right" })
@Html.EditorFor(model => model.PrecioUnitario, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.PrecioUnitario, "", new { @class = "text-danger" })@Html.LabelFor(model => model.Stock, htmlAttributes: new { @class = "col-sm-3 col-md-4 col-lg-2 col-form-label font-weight-bold text-md-right" })
@Html.EditorFor(model => model.Stock, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Stock, "", new { @class = "text-danger" })@Html.LabelFor(model => model.Proveedor, htmlAttributes: new { @class = "col-sm-3 col-md-4 col-lg-2 col-form-label font-weight-bold text-md-right" })
@Html.DropDownListFor(model => model.IdProveedor, (SelectList)ViewBag.ListaProveedores, "", htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.Proveedor, "", new { @class = "text-danger" }) @Html.LabelFor(model => model.Categoria, htmlAttributes: new { @class = "col-sm-3 col-md-4 col-lg-2 col-form-label font-weight-bold text-md-right" })
@Html.DropDownListFor(model => model.IdCategoria, (SelectList)ViewBag.ListaCategorias, "", htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.Categoria, "", new { @class = "text-danger" }) @Html.LabelFor(model => model.Estado, htmlAttributes: new { @class = "col-sm-3 col-md-4 col-lg-2 col-form-label font-weight-bold text-md-right" })
@Html.DropDownListFor(model => model.Estado, (List)ViewBag.ListaEstados, htmlAttributes: new { @class = "form-control" })
|@Html.ActionLink("Regresar al Listado", "Listado")@ViewBag.MensajeExito@ViewBag.MensajeError
@model Entity.Producto @{ ViewBag.Title = "Editar Producto"; }Editar Producto
@using (Html.BeginForm()) { @Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })}
@Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "col-sm-3 col-md-4 col-lg-2 col-form-label font-weight-bold text-md-right" })
@Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })@Html.LabelFor(model => model.Nombre, htmlAttributes: new { @class = "col-sm-3 col-md-4 col-lg-2 col-form-label font-weight-bold text-md-right" })
@Html.EditorFor(model => model.Nombre, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Nombre, "", new { @class = "text-danger" })@Html.LabelFor(model => model.Descripcion, htmlAttributes: new { @class = "col-sm-3 col-md-4 col-lg-2 col-form-label font-weight-bold text-md-right" })
@Html.TextAreaFor(model => model.Descripcion, new { @class = "form-control", @rows = "3", @cols = "50" }) @Html.ValidationMessageFor(model => model.Descripcion, "", new { @class = "text-danger" })@Html.LabelFor(model => model.PrecioUnitario, htmlAttributes: new { @class = "col-sm-3 col-md-4 col-lg-2 col-form-label font-weight-bold text-md-right" })
@Html.EditorFor(model => model.PrecioUnitario, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.PrecioUnitario, "", new { @class = "text-danger" })@Html.LabelFor(model => model.Stock, htmlAttributes: new { @class = "col-sm-3 col-md-4 col-lg-2 col-form-label font-weight-bold text-md-right" })
@Html.EditorFor(model => model.Stock, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Stock, "", new { @class = "text-danger" })@Html.LabelFor(model => model.Proveedor, htmlAttributes: new { @class = "col-sm-3 col-md-4 col-lg-2 col-form-label font-weight-bold text-md-right" })
@Html.DropDownListFor(model => model.IdProveedor, (SelectList)ViewBag.ListaProveedores, "", htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.Proveedor, "", new { @class = "text-danger" }) @Html.LabelFor(model => model.Categoria, htmlAttributes: new { @class = "col-sm-3 col-md-4 col-lg-2 col-form-label font-weight-bold text-md-right" })
@Html.DropDownListFor(model => model.IdCategoria, (SelectList)ViewBag.ListaCategorias, "", htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.Categoria, "", new { @class = "text-danger" }) @Html.LabelFor(model => model.Estado, htmlAttributes: new { @class = "col-sm-3 col-md-4 col-lg-2 col-form-label font-weight-bold text-md-right" })
@Html.DropDownListFor(model => model.Estado, (List)ViewBag.ListaEstados, htmlAttributes: new { @class = "form-control" })
|@Html.ActionLink("Regresar al Listado", "Listado")@ViewBag.MensajeExito@ViewBag.MensajeError
No comments:
Post a Comment