2014-01-31 43 views
12

我想運行我的第一個ASP.NET MVC應用程序。我創建了一個cotroller和視圖。數據來自數據庫。但是,當項目可以運行,但是當我嘗試導航客戶頁面時,出現以下錯誤。如何解決傳入字典的模型項目類型錯誤?

傳遞到詞典中的模型產品 類型的「System.Collections.Generic.List`1 [MvcApplication3.Models.Customer]」, 但本詞典需要 類型「MvcApplication3的模型項。 Models.Customer」。

我有點困惑,因爲錯誤表示它有請求模型類型。

堆棧跟蹤是

堆棧跟蹤:

[InvalidOperationException異常:傳遞到字典 模型產品 類型的「System.Collections.Generic.List 1[MvcApplication3.Models.Customer]', but this dictionary requires a model item of type 'MvcApplication3.Models.Customer'.]
System.Web.Mvc.ViewDataDictionary
1.SetModel(對象值)585211
System.Web.Mvc.ViewDataDictionary..ctor(的ViewDataDictionary字典) 371 System.Web.Mvc.ViewPage 1.SetViewData(ViewDataDictionary viewData) +48 System.Web.Mvc.WebFormView.RenderViewPage(ViewContext context, ViewPage page) +73
System.Web.Mvc.WebFormView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +38
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +295 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +23 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func
1續)242
系統。 Web.Mvc。 <> C_ DisplayClass1c.b _19() 21 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext,IList的1 filters, ActionResult actionResult) +177
System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +89 System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102 System.Web.Mvc.Async.WrappedAsyncResult
1.End() 57 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction( IAsyncResult的 asyncResult)43
System.Web.Mvc。<> C_ DisplayClass1d.b _18(IAsyncResult的 asyncResult)14
System.Web.Mvc.Async。<> C_ DisplayClass4.b _3( IAsyncResult ar)+23 System.Web.Mvc。 Async.WrappedAsyncResult 1.End() +62
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23 System.Web.Mvc.Async.WrappedAsyncResult
1.End()62
System.Web.Mvc.Controller.EndExecute(IAsyncResult的asyncResult)47
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute( IAsyncResult asyncResult)+10
System.Web.Mvc。 <> c_ DisplayClass8.b _3(IAsyncResult asyncResult)+25
System.Web.Mvc.Async。 <> C_ DisplayClass4.b _3(IAsyncResult的 AR)23 System.Web.Mvc.Async.WrappedAsyncResult`1.End()62
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult的asyncResult) + 47 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult的 結果)9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 9514812 System.Web.HttpApplication .ExecuteStep(IExecutionStep步驟,布爾& completedSynchronously)155

這是我的控制器代碼。

namespace MvcApplication3.Controllers 
{ 
    public class CustomerController : Controller 
    { 
     // 
     // GET: /Customer/ 

     public ActionResult Index() 
     { 
      Models.NorthwindDataContext nwd = new Models.NorthwindDataContext(); 
      return View(nwd.Customers.ToList()); 
     } 

    } 
} 

下面是這個視圖

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication3.Models.Customer>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    Index 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

<h2>Index</h2> 

</asp:Content> 

<asp:Content ID="Content3" ContentPlaceHolderID="FeaturedContent" runat="server"> 
</asp:Content> 

<asp:Content ID="Content4" ContentPlaceHolderID="ScriptsSection" runat="server"> 
</asp:Content> 

任何人都可以給我一個提示修復它?

+0

索引行動回報客戶名單。您的視圖是強類型的客戶列表還是單個客戶實例? – Shyju

+0

在我看來像你通過名單,但有@model客戶,而不是在視圖上列出 –

+0

只是爲了看看這是否工作,用'nwd.Customers.ToList()替換'nwd.Customers.ToList()'FirstOrDefault ()' – Niklas

回答

7

您正嘗試將集合傳遞給爲單個對象設計的視圖。

視圖聲明改爲

Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication3.Models.Customer>>

+2

出於興趣。在正在使用'RenderPartial'的用法中,第二個參數爲'null'時,正在執行的部分是否會從調用頁面中假設模型? –

4

什麼讓你感到困惑?在你的aspx文件中,你已經將該模型定義爲一個客戶,但是將一個列表傳遞給它。

期望模型:

System.Web.Mvc.ViewPage<MvcApplication3.Models.Customer> 

您的數據:

return View(nwd.Customers.ToList()); 

顯然不匹配。

0

傳遞到字典的模型產品類型

System.Collections.Generic.List1[MvcApplication3.Models.Customer]

,但本詞典需要類型的模型項目

MvcApplication3.Models.Customer

類型不匹配。

0

在你相關視圖(即.cshtml文件)查找@model報關:ActionResult需要返回模型類,例如右實例如果您已經定義了

@model Customer 

那麼您的ActionResult需要是Customer類型。 您想返回一個列表,因此您需要定義

@model IEnumerable<Customer> 

在視圖中。

0

我有這個問題,這是一個視圖模型的問題。我有這個代碼在我的MVC項目:

var config = new MapperConfiguration(cfg => 
     { 
      cfg.CreateMap<Author, AuthorViewModel>(); 
     }); 

     IMapper mapper = config.CreateMapper(); 
     var dest = mapper.Map<Author, AuthorViewModel>(author); 

     return View("Form", dest); 

但查看Form.cshtml有錯型號:

@model ORMVC.Models.Author 

所以我把它改爲:

@model ORMVC.ViewModels.AuthorViewModel 
相關問題