部分视图错误

本文关键字:错误 视图 | 更新日期: 2023-09-27 18:18:21

我在博客网站项目工作。我创建部分视图的博客文章。

İt's _MainContent partial view

@model System.Collections.Generic.IEnumerable<Blog.Data.Model.Article>
@foreach (var item in Model)
{
<div class="panel">
    <div class="panel-body">
        <div>
            <h3>
                @Html.ActionLink(item.ArticleTitle, "Index", "Article", new { id = item.ArticleId }, new { })
            </h3>
        </div>
        <div id="img">
            <img width="400" height="300" src="@string.Format("data:{0};base64,{1}",item.ArticleImages.FirstOrDefault().ContentType,Convert.ToBase64String(item.ArticleImages.FirstOrDefault().Content))" />
        </div>
        <div>
            <p>
                @Html.DisplayFor(x=> item.Content)
            </p>
        </div>
    </div>
</div>
}

它是索引页

@model System.Collections.Generic.IEnumerable<Blog.Data.Model.Article>
@{
ViewBag.Title = "Anasayfa";
}
<div class="container">
<div class="col-md-offset-10 col-md-2">
    @Html.Partial("_SideMenuPartial")
</div>
<div class="col-md-10">
    @Html.Partial("_MainContent", Model.FirstOrDefault())
</div>
</div>

当我启动这个程序,然后发生

An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code
Additional information: The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.Article_E29CF5FDD5B2D2829B35531643263744339081C99736858CE61DDB0981D557EE', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Blog.Data.Model.Article]'.

部分视图错误

视图正在等待Article对象的集合:

@model System.Collections.Generic.IEnumerable<Blog.Data.Model.Article>

你传递给它一个单独的Article对象:

@Html.Partial("_MainContent", Model.FirstOrDefault())

相反,为视图提供它期望的集合:

@Html.Partial("_MainContent", Model)

(或该集合的任何子集,但仍然是IEnumerable<Article>)