ASP.NET MVC:Null模型传递给局部视图(ASP.NET MVC : Null model passed to partial view)

我有一个ASP.NET MVC项目。

HomeController.cs

public class HomeController : Controller { private siteDBEntities db = new siteDBEntities(); // GET: Main/Home public ActionResult Index() { return View(); } public PartialViewResult _theLastPost() { var a = (from c in db.Posts orderby c.ID_Post descending select new { c.Title,c.Content}); return PartialView(a.ToList()); } }

Index.cshtml

@model IEnumerable<test1.Models.Post> @{ ViewBag.Title = "Tourism"; Layout = "~/Areas/Main/Views/Shared/_Layout.cshtml"; } @Html.Partial("_theLastPost")

PartialView _theLastPost.cshtml

@model IEnumerable<test1.Models.Post> @foreach (var item in Model) { <h2> @item.Title </h2> <p> @item.Content </p> }

Post.cs这是我的视图模型,我只想从EF6框架获得标题和内容。

public partial class Post { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Post() { this.Comments = new HashSet<Comment>(); this.Keywords = new HashSet<Keyword>(); } public int ID_Post { get; set; } public int ID_Category { get; set; } public int ID_Writer { get; set; } public string Title { get; set; } public string Content { get; set; } public string Url { get; set; } public Nullable<System.TimeSpan> Time { get; set; } public Nullable<System.DateTime> Date { get; set; } public string Index_Image { get; set; } public virtual Category Category { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Comment> Comments { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Keyword> Keywords { get; set; } public virtual Writer Writer { get; set; } }

我想显示最后一篇文章,但它告诉我这个错误

The model item passed into the dictionary is of type 'test1.Models.Post', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[test1.Models.Post]

更新1:使用已修复该错误

@{ Html.RenderAction("_theLastPost"); }

现在我有这个错误:实体或复杂类型'siteDB.Post'不能在LINQ to Entities查询中构造。

修正 :我只是编辑控制器的部分视图(_theLastPost)如下所示:

public PartialViewResult _theLastPost() { var a = (from c in db.Posts orderby c.ID_Post descending select c); return PartialView(a); }

I have a ASP.NET MVC Project .

HomeController.cs

public class HomeController : Controller { private siteDBEntities db = new siteDBEntities(); // GET: Main/Home public ActionResult Index() { return View(); } public PartialViewResult _theLastPost() { var a = (from c in db.Posts orderby c.ID_Post descending select new { c.Title,c.Content}); return PartialView(a.ToList()); } }

Index.cshtml

@model IEnumerable<test1.Models.Post> @{ ViewBag.Title = "Tourism"; Layout = "~/Areas/Main/Views/Shared/_Layout.cshtml"; } @Html.Partial("_theLastPost")

PartialView _theLastPost.cshtml

@model IEnumerable<test1.Models.Post> @foreach (var item in Model) { <h2> @item.Title </h2> <p> @item.Content </p> }

Post.cs this is my view model , that i just want get title and content from EF6 framework .

public partial class Post { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Post() { this.Comments = new HashSet<Comment>(); this.Keywords = new HashSet<Keyword>(); } public int ID_Post { get; set; } public int ID_Category { get; set; } public int ID_Writer { get; set; } public string Title { get; set; } public string Content { get; set; } public string Url { get; set; } public Nullable<System.TimeSpan> Time { get; set; } public Nullable<System.DateTime> Date { get; set; } public string Index_Image { get; set; } public virtual Category Category { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Comment> Comments { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Keyword> Keywords { get; set; } public virtual Writer Writer { get; set; } }

I want show the last post , but it show me this error

The model item passed into the dictionary is of type 'test1.Models.Post', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[test1.Models.Post]

Update 1: That error has fixed by using

@{ Html.RenderAction("_theLastPost"); }

Now i have this error : The entity or complex type 'siteDB.Post' cannot be constructed in a LINQ to Entities query.

Fixed: I just edit Controller for partial view(_theLastPost) like below:

public PartialViewResult _theLastPost() { var a = (from c in db.Posts orderby c.ID_Post descending select c); return PartialView(a); }

最满意答案

在你的情况下,你应该使用RenderAction而不是Partial becouse Partial只是渲染视图。 只要你不通过ViewModel,你会看到这个错误。

RenderAction会调用你的控制器并返回View。

您在索引上的电话将是:

@{@Html.RenderAction("_theLastPost");}

您也可以从Controller anonymouse对象返回。 你不能这样做,因为所有的属性都会被预测,你无法在View上得到它们。

ViewModel应该是这样的:

public class ViewModelPost { public string Title {get; set;} public string Content {get; set;} } public PartialViewResult _theLastPost() { var a = (from c in db.Posts orderby c.ID_Post descending select new ViewModelPost { c.Title,c.Content}); return PartialView(a.ToList()); }

并在您的观点:

@model IEnumerable<ViewModelPost>

In your case you should use RenderAction not Partial becouse Partial just renders View. And as long as you don't pass ViewModel there you see this error.

While RenderAction will call your Controller and returns View.

Your call on Index will be:

@{@Html.RenderAction("_theLastPost");}

Also you return from your Controller anonymouse object. You can't do it becouse all properties vill be propected and you can't get them on View.

It should be like this with ViewModel:

public class ViewModelPost { public string Title {get; set;} public string Content {get; set;} } public PartialViewResult _theLastPost() { var a = (from c in db.Posts orderby c.ID_Post descending select new ViewModelPost { c.Title,c.Content}); return PartialView(a.ToList()); }

And on your View:

@model IEnumerable<ViewModelPost>

更多推荐

public,Post,_theLastPost,set,电脑培训,计算机培训,IT培训"/> <meta name="