Estoy tratando de mostrar datos de SQL en un pdf mediante MVC EF pero me sale el siguiente error al clickear para obtener los datos:
El elemento de modelo pasado al diccionario es de tipo 'ProviEntity.V_CuetaWeb', pero este diccionario requiere un elemento de modelo de tipo 'System.Collections.Generic.IEnumerable`1[ProviEntity.V_CuetaWeb]'.
He intentado varias opciones pero no he dado con ninguna que me funcione, agradecería cualquier tipo de ayuda o consejo o comentario.
Clase:
public partial class V_CuetaWeb
{
public string Agencia { get; set; }
public string Cobrador { get; set; }
public string Nombre { get; set; }
public string Apellido { get; set; }
public Nullable<double> Titulo { get; set; }
public Nullable<double> Endoso { get; set; }
public string Domicilio { get; set; }
public string Numero { get; set; }
public string Piso { get; set; }
public string Dpto { get; set; }
public string Mono { get; set; }
public string DniTipo { get; set; }
public double DniNumero { get; set; }
public string Barrio { get; set; }
public string Localidad { get; set; }
public string Provincia { get; set; }
public string Telefono { get; set; }
public string Celular { get; set; }
public string acidzona { get; set; }
public Nullable<double> Sorteo { get; set; }
public Nullable<double> Cuota { get; set; }
public string Codigo { get; set; }
public Nullable<decimal> Monto { get; set; }
public Nullable<int> CodigoPostal { get; set; }
public string barras { get; set; }
public Nullable<int> deuda { get; set; }
public string Solicitud { get; set; }
public Nullable<System.DateTime> Vencimiento { get; set; }
public string Estado { get; set; }
public Nullable<double> ValNom { get; set; }
public Nullable<double> PlanAntes { get; set; }
public string DescPlanActual { get; set; }
public System.DateTime FechaSorteo { get; set; }
public string suscriptor { get; set; }
}
Controlador:
public ActionResult Index(string buscar, string buscarCuota)
{
PDFPrinter db = new PDFPrinter();
ViewBag.CurrentFilter = buscar;
ViewBag.CurrentFilter = buscarCuota;
var datos = from s in db.V_CuetaWeb
select s;
if (!String.IsNullOrEmpty(buscar) || !String.IsNullOrEmpty(buscarCuota))
{
datos = datos.Where(s => s.Titulo.ToString().Contains(buscar.ToUpper())
|| s.Cuota.ToString().Contains(buscarCuota.ToUpper()));
}
return View(datos.ToList());
}
public ActionResult PrintViewToPdf()
{
var report = new ActionAsPdf("Index");
return report;
}
public ActionResult PrintPartialViewToPdf(double id)
{
using (PDFPrinter db = new PDFPrinter())
{
V_CuetaWeb customer = db.V_CuetaWeb.FirstOrDefault(c => c.Titulo == id);
var report = new PartialViewAsPdf("DetailCustomer", customer);
return report;
}
}
Index:
@model IEnumerable<ProviEntity.V_CuetaWeb>
@{
ViewBag.Title = "Inicio";
}
<p>
@Html.ActionLink("Convert View To PDF", "PrintViewToPdf")
</p>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Inicio</title>
</head>
<body>
<form id="form">
<div>
@using (Html.BeginForm())
{
<p>
Título: @Html.TextBox("buscar", ViewBag.CurrentFilter as string)
<input type="submit" value="Filtrar" /><br />
Cuota: @Html.TextBox("buscarCuota", ViewBag.CurrentFilter as string)
<input type="submit" value="Filtrar" /><br />
<input type="button" value="Imprimir" onclick="window.print()" />
</p>
}
</div>
<div>
<table border="1">
@foreach (var item in Model)
{
<tr>
<th scope="row" abbr="Suscriptor">Suscriptor: </th>
<td>
<b>@Html.DisplayFor(modelItem => item.Apellido), @Html.DisplayFor(modelItem => item.Nombre)</b>
</td>
<td></td>
<td></td>
<td></td>
<td>Título: @Html.DisplayFor(modelItem => item.Titulo)</td>
</tr>
<td>
@Html.ActionLink("Print Details View To PDF", "PrintPartialViewToPdf", new { id = item.Titulo})
</td>
}
</table>
</div>
</form>
</body>
</html>
DetailCustomer: (Aquí está el problema, ya que al acceder a esta parte sale el error)
@model IEnumerable<ProviEntity.V_CuetaWeb>
@{
ViewBag.Title = "DetailCustomer";
}
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Inicio</title>
</head>
<body>
<form id="form">
<div>
<table border="1">
@foreach (var item in Model)
{
<tr>
<th scope="row" abbr="Domicilio">Domicilio: </th>
<td>
@Html.DisplayFor(modelItem => item.Domicilio)
</td>
<td></td>
<td></td>
<td></td>
<td>Valor Nominal: @Html.DisplayFor(modelItem => item.ValNom)</td>
</tr>
<tr>
<th scope="row" abbr="Barrio">Barrio: </th>
<td>
@Html.DisplayFor(modelItem => item.Barrio)
</td>
</tr>
<tr>
<th scope="row" abbr="Localidad">Localidad: </th>
<td>
@Html.DisplayFor(modelItem => item.Localidad)
</td>
</tr>
<tr>
<th scope="row" abbr="Telefono">Teléfono: </th>
<td>
@Html.DisplayFor(modelItem => item.Telefono)
</td>
</tr>
<tr>
<th scope="row" abbr="Celular">Celular: </th>
<td>
@Html.DisplayFor(modelItem => item.Celular)
</td>
</tr>
<tr>
<th scope="row" abbr="Descripcion">D. Plan Actual: </th>
<td>
@Html.DisplayFor(modelItem => item.DescPlanActual)
</td>
</tr>
<tr>
<th scope="row" abbr="Fecha">Fecha Sorteo: </th>
<td>
@Html.DisplayFor(modelItem => item.FechaSorteo)
</td>
</tr>
<tr>
<th>Zona: @Html.DisplayFor(modelItem => item.acidzona)</th>
<th>Cobrador: @Html.DisplayFor(modelItem => item.Cobrador)</th>
<th>Código: @Html.DisplayFor(modelItem => item.Codigo)</th>
<th>Título: @Html.DisplayFor(modelItem => item.Titulo)/@Html.DisplayFor(modelItem => item.Endoso)</th>
<th>Sorteo: @Html.DisplayFor(modelItem => item.Sorteo)</th>
<th>Cuota: @Html.DisplayFor(modelItem => item.Cuota)</th>
<th>Vencimiento: @Html.DisplayFor(modelItem => item.Vencimiento)</th>
<th>Monto: @Html.DisplayFor(modelItem => item.Monto)</th>
</tr>
}
</table>
</div>
</form>
</body>
</html>
Dejo imagen (Al presionar en Print Details View To PDF):
System.Collections.Generic
? – Luis Cazares el 14 jun. 19 a las 17:29PrintPartialViewToPdf
cuando obtienes los datos haces unFirstOrDefault
, por lo tanto solo estas obteniendo un objeto y no una lista de objeto, Y en tu vistaDetailCustomer
estas esperando que el modelo sea una lista deV_CuetaWeb
, pero desde el controlador solo envias un objeto plano deV_CuetaWeb
. – Orlando De La Rosa el 14 jun. 19 a las 17:44DetailCustomer
– fredyfx el 14 jun. 19 a las 17:47