2011-12-06 149 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"); 
}