2014-12-09 66 views
4

我得到这个错误,我不知道如果我能做到这一点,这里是我的代码..MVC:词典需要类型的模型项目“System.Collections.Generic.IEnumerable`1

应用控制器

public ActionResult AppView() 
{ 
    List<Application> apps; 
    using (ISiteDbContext context = _resolver.GetService<ISiteDbContext>()) 
    { 
     apps = context.Applications.ToList(); 
    } 
    return PartialView("AppView", apps.OrderBy(a => a.Name).ToList()); 
} 

渲染部分 - 这是一个视图,其是在家庭控制器内。

@{Html.RenderPartial("~/Views/Application/AppView.cshtml", new Example.Services.DAL.Application());} 

和我的应用程序视图

@model IEnumerable<Example.Services.DAL.Application> 

@{ 
    ViewBag.Title = "Applications"; 
} 

<h2>Applications</h2> 

<p> 
    @Html.ActionLink("Add New Application", "Create") 
</p> 
<table> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.Name) 
     </th> 
     <th></th> 
    </tr> 

    @foreach (var item in Model) 
    { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.Name) 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "Edit", new { id = item.ID }) | 
      @Html.ActionLink("Details", "Details", new { id = item.ID }) | 
      @Html.ActionLink("Delete", "Delete", new { id = item.ID }) 
     </td> 
    </tr> 
    } 

</table> 

完整的错误信息:

传递到字典的模型产品的类型 'Example.Services.DAL.Application',但本字典需要 型号项目 'System.Collections.Generic.IEnumerable`1 [Example.Services.DAL.Application]'。

+1

请在你的问题的身体*全*错误消息。完整的错误信息不会以“but”开头,但会加入 – 2014-12-09 11:51:58

+1

完整的错误信息。 – 2014-12-09 11:54:38

回答

3

由于错误状态您传递了错误的类型。更改

@{Html.RenderPartial("~/Views/Application/AppView.cshtml", new Example.Services.DAL.Application());} 

到:

@{Html.RenderPartial("~/Views/Application/AppView.cshtml", new List<Example.Services.DAL.Application> { new Example.Services.DAL.Application() });} 
+0

啊谢谢你! :) – 2014-12-09 11:58:07

+0

很好的回答!它可以工作,但是它在呈现局部视图时会自动向列表中添加新行? – Nurul 2017-09-19 04:50:45

+0

@Nurul不知道如果我100%理解你的问题,但基于问题的AppView.cshtml'应该添加一个新行 - 你传递'Application'的集合,并且它列出所有项目(在这个例子中它只是一行)。 – Vladimirs 2017-09-19 10:20:59

2

AppView.cshtml被绑定到强类型的@model IEnumerable<Example.Services.DAL.Application>并同时呼吁这个观点你逝去的@{Html.RenderPartial("~/Views/Application/AppView.cshtml", new Example.Services.DAL.Application());}

它应该是列表对象。你必须通过Example.Services.DAL.Application()

list

更改

@{Html.RenderPartial("~/Views/Application/AppView.cshtml", new Example.Services.DAL.Application());} 

@{Html.RenderPartial("~/Views/Application/AppView.cshtml", new List<Example.Services.DAL.Application> { new Example.Services.DAL.Application() });} 
0

为了使用上的(自定义)对象的集合排序,你需要为它进行排序的方法。通常,这是通过继承“IComparable”接口来完成的。在你的Object类中,你需要一个“比较”的方法来确定比较你的对象实例进行排序的方法(我在我的项目中使用“Date”)。

总结一下:

你在你的应用程序控制器使用:

回报PartialView( “APPVIEW”,apps.OrderBy(A => a.Name).ToList());

但为了实际排序(或在本例中为OrderBy),您需要在“Application”类中比较列表中的实例对它们进行排序的方法。

INT比较(对象X,对象Y)

你怎么比较是完全取决于你:这是用 “比较” 的方法来完成。然而,结果可以是:

  • 小于零:对象x <对象ý
  • 零:对象x =对象ý
  • 大于零:对象x>对象ÿ

我希望这有助于。祝你好运!

尊敬的问候, 比约恩

1

你的代码是寻找一个IEnumerable使用,因为你通过你的部分观点是什么,必须是相同的东西在你的视图,以便尝试您的应用程序视图的第一行更改为

@model Example.Services.DAL.Application 

它的工作对我希望它是用好你的还有:d

相关问题