I am using .NET 4.0, MVC3 and entity framework 4.0
I need to pass a database record of type “statement” to a view. The code to fetch the record is as below:
public ActionResult pdfStatement(string InvoiceNumber)
{
ObjectParameter[] parameters = new ObjectParameter[1];
parameters[0] = new ObjectParameter("InvoiceNumber", InvoiceNumber);
return View(_db.ExecuteFunction<Models.Statement>("uspInvoiceStatement", parameters));
}
where “uspInvoiceStatement” is my stored procedure used to fetch “statement”
I have created a strongly typed view which receives the “statement”
< % @ Page Language="C#" Inherits="System.Web.Mvc.ViewPage < InvoiceSearch.Models.Statement >" %>
When i run the application i get an exception saying
"The model item passed into the dictionary is of type 'System.Data.Objects.ObjectResult`1[InvoiceSearch.Models.Statement]',
but this dictionary requires a model item of type 'InvoiceSearch.Models.Statement'.
Help me to escape from this trouble.
“
The error is surprisingly helpful – it tells you, you are sending ObjectResult<Statement> to the view asi viewmodel, while you should send Statement.
Your _db.ExecuteFunction always returns ObjectResult<Statement>, so you need to take Statement from it, like this :