@model SPTC.NIE.SPG.Application.ViewModels.UsuarioViewModel
@{ ViewBag.Title = "Index"; }
Index
@model SPTC.NIE.SPG.Application.ViewModels.UsuarioViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.TipoUsuarioViewModel.Descricao)
</th>
<th>
@Html.DisplayNameFor(model => model.Nome)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.Ativo)
</th>
<th>
@Html.DisplayNameFor(model => model.DataCadastro)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.TipoUsuarioViewModel.Descricao)
</td>
<td>
@Html.DisplayFor(modelItem => item.Nome)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.Ativo)
</td>
<td>
@Html.DisplayFor(modelItem => item.DataCadastro)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.UsuarioId }) |
@Html.ActionLink("Details", "Details", new { id=item.UsuarioId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.UsuarioId })
</td>
</tr>
}
</table>
@Html.ActionLink("Create New", "Create")
@Html.DisplayNameFor(model => model.TipoUsuarioViewModel.Descricao)@Html.DisplayNameFor(model => model.Nome)@Html.DisplayNameFor(model => model.Email)@Html.DisplayNameFor(model => model.Ativo)@Html.DisplayNameFor(model => model.DataCadastro)@foreach (var item in Model) { @Html.DisplayFor(modelItem => item.TipoUsuarioViewModel.Descricao) @Html.DisplayFor(modelItem => item.Nome) @Html.DisplayFor(modelItem => item.Email) @Html.DisplayFor(modelItem => item.Ativo) @Html.DisplayFor(modelItem => item.DataCadastro) @Html.ActionLink("Edit", "Edit", new { id=item.UsuarioId }) | @Html.ActionLink("Details", "Details", new { id=item.UsuarioId }) | @Html.ActionLink("Delete", "Delete", new { id=item.UsuarioId }) }
using System.Web.Mvc; using SPTC.NIE.SPG.Application.ViewModels; using System.Threading.Tasks; using SPTC.NIE.SPG.Application.Interfaces;
namespace SPTC.NIE.ProtocoloGeral.MVC.Controllers { public class UsuariosController : Controller { private readonly IUsuarioAppService _usuarioApp; private readonly ITipoUsuarioAppService _tipoUsuarioApp; public UsuariosController() {
using System.Web.Mvc;
using SPTC.NIE.SPG.Application.ViewModels;
using System.Threading.Tasks;
using SPTC.NIE.SPG.Application.Interfaces;
namespace SPTC.NIE.ProtocoloGeral.MVC.Controllers
{
public class UsuariosController : Controller
{
private readonly IUsuarioAppService _usuarioApp;
private readonly ITipoUsuarioAppService _tipoUsuarioApp;
public UsuariosController()
{
}
public UsuariosController(IUsuarioAppService usuarioApp, ITipoUsuarioAppService tipoUsuarioApp)
{
_usuarioApp = usuarioApp;
_tipoUsuarioApp = tipoUsuarioApp;
}
// GET: Usuarios
public ActionResult Index()
{
return View();
}
// GET: Usuarios/Details/5
public ActionResult Details(int id)
{
var usuarioViewModel = _usuarioApp.GetById(id);
if (usuarioViewModel == null)
{
return HttpNotFound();
}
return View(usuarioViewModel);
}
// GET: Usuarios/Create
public async Task<ActionResult> Create()
{
ViewBag.TipoUsuarioId = new SelectList(await _tipoUsuarioApp.GetAll(), "TipoUsuarioId", "Descricao");
return View();
}
// POST: Usuarios/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(UsuarioViewModel usuario)
{
if (ModelState.IsValid)
{
ViewBag.TipoUsuarioId = new SelectList(await _tipoUsuarioApp.GetAll(), "TipoUsuarioId", "Descricao");
_usuarioApp.Add(usuario);
return RedirectToAction("Index");
}
return View(usuario);
}
// GET: Usuarios/Edit/5
public async Task<ActionResult> Edit(int UsuarioId)
{
var tipo = await _tipoUsuarioApp.GetAll();
var usuario = _usuarioApp.GetById(UsuarioId);
if(usuario == null)
{
return HttpNotFound();
}
ViewBag.TipoUsuarioId = new SelectList(tipo, "TipoUsuarioId", "Descricao");
return View(usuario);
}
// POST: Usuarios/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(UsuarioViewModel usuario)
{
if (ModelState.IsValid)
{
_usuarioApp.Update(usuario);
return RedirectToAction("Index");
}
//ViewBag.TipoUsuarioId = new SelectList(db.TipoUsuarioViewModels, "TipoUsuarioId", "Descricao", usuario.TipoUsuarioId);
return View(usuario);
}
//protected override void Dispose(bool disposing)
//{
// if (disposing)
// {
// _usuarioApp.Dispose();
// }
// base.Dispose(disposing);
//}
}
}
}
Classe ViewModel: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations;
namespace SPTC.NIE.SPG.Application.ViewModels { public class UsuarioViewModel { [Key] public int UsuarioId { get; set; } public int TipoUsuarioId { get; set; }Classe ViewModel:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace SPTC.NIE.SPG.Application.ViewModels
{
public class UsuarioViewModel
{
[Key]
public int UsuarioId { get; set; }
public int TipoUsuarioId { get; set; }
[Display(Name ="Nome do Usuário")]
[Required(ErrorMessage ="O nome é obrigatório.")]
[MaxLength(60, ErrorMessage ="O Nome do Usuário poderá ter no máximo 60 caracteres.")]
public string Nome { get; set; }
[Display(Name = "E-mail")]
[Required(ErrorMessage = "O E-mail é obrigatório.")]
[MaxLength(80, ErrorMessage = "O E-mail poderá ter no máximo 80 caracteres.")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
public bool Ativo { get; set; }
[Display(Name = "Data de Cadasatro")]
public DateTime DataCadastro { get; set; }
public virtual TipoUsuarioViewModel TipoUsuarioViewModel { get; set; }
public virtual ICollection<ExpedienteUsuarioViewModel> ExpedienteUsuariosViewModel { get; set; }
}
}
}