部分视图出错

时间:2016-11-17 16:03:50

标签: c# asp.net asp.net-mvc

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

İt的_MainContent局部视图

@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]'.

我有一个空页面。

1 个答案:

答案 0 :(得分:3)

该视图需要一组Article个对象:

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

您正在传递一个Article对象:

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

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

@Html.Partial("_MainContent", Model)

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