2011-12-06 65 views
0

一个观点我使用.NET 4.0,MVC3和Entity Framework 4.0通过历史使用实体框架

我需要键入“声明”的数据库记录传递给视图。来获取该记录的代码如下:

public ActionResult pdfStatement(string InvoiceNumber) 

{ 

      ObjectParameter[] parameters = new ObjectParameter[1]; 

      parameters[0] = new ObjectParameter("InvoiceNumber", InvoiceNumber); 

      return View(_db.ExecuteFunction<Models.Statement>("uspInvoiceStatement", parameters));    
     } 

其中“uspInvoiceStatement”是用来获取“声明”

我创建了接收“声明”

一个强类型的视图我的存储过程
< % @ Page Language="C#" Inherits="System.Web.Mvc.ViewPage <InvoiceSearch.Models.Statement>" %> 

当我运行该应用程序我得到一个异常说

"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'. 

帮助我摆脱这种麻烦。 “

回答

0

错误是令人惊讶的有益的 - 它告诉你,你要发送ObjectResult <声明>到视图ASI视图模型,而你应该送声明

你_db.ExecuteFunction总是返回ObjectResult <声明>。所以你需要从它的声明,像这样:

var statementResult = _db.ExecuteFunction<Models.Statement>("uspInvoiceStatement", parameters); 
var statement = statementResult.SingleOrDefault(); 
if (statement != null) 
{ 
    return View(statement); // isnt that nicer when you can see what your viewmodel is? 
} 
else 
{ 
    return View("NotFound"); 
}