I changed the codes from the previous question, to use a string that makes things easier for me. I got what I was looking for. Now what is generated is the following exception:
System.InvalidOperationException: 'The entity type ProvidusCuotas is not part of the model for the current context.'
Controller:
public ActionResult Index(){
SQL sql = new SQL();
List<ProvidusCuotas> lista = sql.cargarDatos();
return View(lista);
}
public ActionResult PrintViewToPdf(){
var report = new ActionAsImage("Index");
return report;
}
public ActionResult PrintPartialViewToPdf(string id){
using (PDFPrinter db = new PDFPrinter()) {
ProvidusCuotas customer = db.SQLs.FirstOrDefault(c => c.titulo == id); //justo en esta línea se genera la excepción
List<ProvidusCuotas> lista = new List<ProvidusCuotas>();
lista.Add(customer);
var report = new PartialViewAsPdf("Detalle", lista);
return report;
}
}
SQL class:
public List<ProvidusCuotas> cargarDatos(){
List<ProvidusCuotas> salida = new List<ProvidusCuotas>();
SqlConnection conn = new SqlConnection("conexion");
conn.Open();
SqlCommand comand = new SqlCommand("SELECT titulo, apellido +', '+nombre,max(Cuota) FROM V_CuetaWeb GROUP BY titulo, apellido, nombre ORDER BY titulo ASC", conn);
SqlDataReader dr = comand.ExecuteReader();
while (dr.Read()){
string titulo = Convert.ToString(dr.GetDouble(0));
string nombre = dr.GetString(1);
string cuota = Convert.ToString(dr.GetDouble(2));
ProvidusCuotas p = new ProvidusCuotas(titulo,nombre, cuota);
salida.Add(p);
}
conn.Close();
return salida;
}
PDFPrinter class:
public partial class PDFPrinter : DbContext
{
public PDFPrinter()
: base("name=PDFPrinter")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
//public virtual DbSet<V_CuetaWeb> V_CuetaWeb { get; set; }
public virtual DbSet<ProvidusCuotas> SQLs { get; set; }
}
The problem arises when in the INDEX I click on this link:
<td>
@Html.ActionLink("Azul","PrintPartialViewToPdf", new { id = item.titulo})
</td>
The idea is that when clicking on that link, a pdf is loaded with the information I need but the exception mentioned above arises
If you have the context with the mapped entity because you don't use linq
so you will generate a sql applying the query instead of having a string that defines the sql
You can try this: