Has anyone ever had an error like this?
View Index Code:
@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>
Controller:
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);
//}
}
}
ViewModel class:
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; }
}
}