I am trying to create a new client, in case it is successful, it should return to the main screen where all the clients are. But when trying to do it I get the error screen with the following error
The model element passed to the dictionary is of type 'System.Collections.Generic.List`1[SegurosDeVida.Models.DTO_Cliente]', but this dictionary requires a model element of type 'SegurosDeVida.Models.DTO_Cliente'.
This is the code for the create view, which I use to create the new client:
@model SegurosDeVida.Models.DTO_Cliente
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm("Create", "Cliente"))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>DTO_Cliente</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Identificacion, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Identificacion, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Identificacion, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Nombre, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Nombre, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Nombre, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Apellido, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Apellido, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Apellido, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Edad, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Edad, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Edad, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Volver Atras", "Index","Cliente")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
This is the code of the main view
@model IEnumerable<SegurosDeVida.Models.DTO_Cliente>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Nuevo Cliente", "Create","Cliente")>
</p>
<table class="table">
<tr>
<th>
Numero de Cliente
</th>
<th>
Identificacion
</th>
<th>
Nombre
</th>
<th>
Apellido
</th>
<th>
Edad
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.IdCliente)
</td>
<td>
@Html.DisplayFor(modelItem => item.Identificacion)
</td>
<td>
@Html.DisplayFor(modelItem => item.Nombre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Apellido)
</td>
<td>
@Html.DisplayFor(modelItem => item.Edad)
</td>
<td>
@Html.ActionLink("Editar", "Edit", "Cliente") |
@Html.ActionLink("Eliminar", "Delete", "Cliente") |
</td>
</tr>
}
</table>
This is the code for the client controller, which I use to load clients on the main screen, as well as create new clients:
namespace SegurosDeVida.Controllers
{
public class ClienteController : Controller
{
ServiceCliente service = null;
[HttpGet]
public ActionResult Index()
{
service = new ServiceCliente();
return View(service.GetClientes(true));
}
[HttpGet]
public ActionResult Create()
{
service = new ServiceCliente();
return View();
}
[HttpPost]
public ActionResult Create(string Identificacion, string Nombre, string Apellido, string Edad)
{
service = new ServiceCliente();
bool exito;
DTO_Cliente dto = new DTO_Cliente();
DTO_Atributos dtoa = new DTO_Atributos();
IList<DTO_Atributos> lista = new List<DTO_Atributos>();
dtoa.IdAtributo = "1";
dtoa.Titulo = "Maneja";
dtoa.Valor = false;
dtoa.Observaciones = "nada";
lista.Add(dtoa);
dtoa.IdAtributo = "2";
dtoa.Titulo = "Usa Lentes";
dtoa.Valor = false;
dtoa.Observaciones = "nada";
lista.Add(dtoa);
dto.Identificacion = Identificacion;
dto.Nombre = Nombre;
dto.Apellido = Apellido;
dto.Edad = Edad;
dto.Activo = true;
exito = service.RegistrarCliente(dto,lista);
if (exito)
{
ViewBag.res = "Clientes registrado con exito";
ViewBag.Alerta = "Success";
}
else
{
ViewBag.res = "Error al registrar el cliente, intenten nuevamente";
ViewBag.Alerta = "Danger";
}
return Index();
}
[HttpPost]
public ActionResult Delete(string IdCliente)
{
service = new ServiceCliente();
service.EliminarCliente(Convert.ToInt32(IdCliente));
return Index();
}
}
}
As you can see, when registering the new client, I call Index()
, which calls a service.GetClientes(true))
that returns an IList<DTO_Client>. When I load the main screen, it brings me the clients without problems as seen:
but when calling Index()
, from the creation view, it throws me the aforementioned error. I would be grateful if you could tell me where my error is, surely it is something nonsense, but I am just starting out in ASP.Net and I don't see where the error is.