【问题标题】:ASP.NET MVC : Null model passed to partial viewASP.NET MVC:空模型传递给局部视图
【发布时间】:2015-12-11 12:30:15
【问题描述】:

我有一个 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());
    }
}

索引.cshtml

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

部分视图_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”。

已修复: 我只是为部分视图(_theLastPost)编辑控制器,如下所示:

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

【问题讨论】:

  • 试试@model IEnumerable
  • 如果你的意思是在局部视图中,我之前添加了它
  • TBH:提供的代码与提供的错误不匹配。你改变了什么吗?看起来您的视图模型不是 IEnumerable 或您的 @Html.Partial 调用与所述不符
  • 如您所见,模型已传递给局部视图是 IEnumerable @freedomn-m

标签: asp.net-mvc asp.net-mvc-4


【解决方案1】:

在您的情况下,您应该使用RenderAction 而不是Partial 因为Partial 只是呈现视图。只要您不在那里传递 ViewModel,您就会看到此错误。

RenderAction 将调用您的控制器并返回视图。

您对 Index 的调用将是:

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

你也从你的控制器匿名对象返回。你不能这样做,因为所有属性都会被预测,而你无法在 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>

【讨论】:

  • 什么时候使用 @Html.RenderAction("_theLastPost"); ,错误:无法将隐式“void”转换为“object”
  • 你有没有在 helper 周围提到 @{ }?你需要它
  • @AlirezaSoleimaniAsl 你确定吗?我的意思是我相信你现在有提到的问题here
  • 是的。但现在我收到此错误:执行处理程序'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'的子请求时出错。
  • @freedomn-m 但this 告诉反对者。有什么想法吗?
【解决方案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

【讨论】:

  • 我设置了这样的模型 @model test1.Models.Post 但我收到此错误:对象引用未设置为对象的实例。
  • 那是一个单独的问题。调试它。您的问题已得到解答。
  • 这太疯狂了。我只花了 15 分钟对着视觉工作室大喊它在骗我,直到我意识到我通过的模型是空的。
猜你喜欢
  • 1970-01-01
  • 2013-07-14
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
  • 2012-02-02
  • 1970-01-01
  • 2013-02-15
相关资源
最近更新 更多