我有一个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"); }
现在我有这个错误: 无法在LINQ to Entities查询中构造实体或复杂类型“siteDB.Post”。
固定: 我只是编辑Controller for partial view(_theLastPost),如下所示:
public PartialViewResult _theLastPost()
{
var a = (from c in db.Posts
orderby c.ID_Post descending
select c);
return PartialView(a);
}
答案 0 :(得分:3)
在您的情况下,您应该使用RenderAction
而不是Partial
因为Partial
只是呈现视图。只要你没有通过ViewModel就可以看到这个错误。
虽然RenderAction
会调用您的Controller并返回View。
您对Index的致电将是:
@{@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>
答案 1 :(得分:2)
原因:
如果您没有为@Partial
指定模型,则会使用视图模型:
@Html.Partial("_theLastPost")
与
相同@Html.Partial("_theLastPost", Model)
当视图/部分@model
不相同时,会出现此错误。
修复:
由于您的部分控制器操作已经执行了您想要的操作,请通过@Html.Action
或者,传递您想要的数据。
假设您的视图模型包含数据(它不在提供的代码中):
@Html.Partial("_thisLastPost", Model.OrderByDescending(x=>x.ID_Post).First())
(但请确保部分只需要一个项目:@model test1.Models.Post
)