【发布时间】:2019-07-04 15:03:54
【问题描述】:
我在_ShowComments.cshtml 视图中将模型定义为元组类型,但是当我想调用这个 Partialview 时
当我在Default.cshtml 中调用该方法时出现此错误。
我该如何解决?
错误信息:
InvalidOperationException:传入的模型项 ViewDataDictionary 是类型 'System.ValueTuple`2[System.Collections.Generic.List`1[Jahan.Beta.Web.App.Models.Comment],System.Nullable`1[System.Int32]]', 但是这个 ViewDataDictionary 实例需要一个类型的模型项 'System.ValueTuple`2[System.Collections.Generic.IList`1[Jahan.Beta.Web.App.Models.Comment],System.Nullable`1[System.Int32]]'。
Default.cshtml:
@model List<Comment>
<div class="media mb-4">
<div class="media-body">
@Html.Partial("_ShowComments", ValueTuple.Create<List<Comment>, int?>(Model,null))
</div>
</div>
_ShowComments.cshtml:
@model (IList<Comment> comments, int? parentId)
@if (Model.comments.Any(c => c.ParentId == Model.parentId))
{
<ul class="list-unstyled">
@foreach (var childComment in Model.comments.Where(c => c.ParentId == Model.parentId))
{
<li class="media">
@Html.Partial("_ShowComments", (Model.comments, childComment.Id))
</li>
}
</ul>
}
【问题讨论】:
标签: c# asp.net-core tuples asp.net-core-2.1