En las partes anteriores de esta entrada, aquí vimos las vistas [Listado y Ver] y aquí las vistas [Crear y Editar]
La vista Eliminar luce como sigue:
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 Eliminar(int id)
{
Producto p = new Producto();
p = _business.GetById(id);
return View(p);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Eliminar(int id, Producto p)
{
try
{
if (ModelState.IsValid)
{
if (_business.Delete(id))
{
WriteMessage(Eoperacion.delete, Etipo.success);
ModelState.Clear();
}
else
{
WriteMessage(Eoperacion.delete, Etipo.danger);
}
}
}
catch (DataException)
{
ModelState.AddModelError("", "Unable to delete the record.");
WriteMessage(Eoperacion.delete, 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
}
}
De igual manera como los otros métodos, clic derecho y agregar vista.Vista Eliminar Producto
@model Entity.Producto
@{
ViewBag.Title = "Eliminar Producto";
}
Eliminar Producto
Eliminar ¿Está seguro que desea eliminar el producto @(Model.Nombre)?
@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.EditorFor(model => model.Proveedor, new { 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.EditorFor(model => model.Categoria, new { 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.EditorFor(model => model.Nombre_Estado, new { htmlAttributes = new { @class = "form-control" } })
|@Html.ActionLink("Regresar al Listado", "Listado")
}
@ViewBag.MensajeExito
@ViewBag.MensajeError

No comments:
Post a Comment