Я определил модель в представлении _ShowComments.cshtml как тип кортежа, но когда я хочу вызвать это частичное представление
Я получаю эту ошибку, когда я вызываю этот метод в Default.cshtml.
Как я могу это решить?
Сообщение об ошибке:
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.ValueTuple`2[System.Collections.Generic.List`1[Jahan.Beta.Web.App.Models.Comment],System.Nullable`1[System.Int32]]', but this ViewDataDictionary instance requires a model item of type 'System.ValueTuple`2[System.Collections.Generic.IList`1[Jahan.Beta.Web.App.Models.Comment],System.Nullable`1[System.Int32]]'.
По умолчанию.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>
}





Вы создаете ValueTuple<List<Comment>, int?>, когда представление ожидает ValueTuple<IList<Comment>, int?> (обратите внимание на List и IList), и компилятор видит их как разные типы. Используйте правильный тип кортежа:
@Html.Partial("_ShowComments", ValueTuple.Create<IList<Comment>, int?>(Model,null))
Или, на мой взгляд, более чистый синтаксис:
@Html.Partial("_ShowComments", ((IList<Comment>)Model,null))
Или, мое предпочтительное решение, создайте правильный класс для хранения значений:
public class ShowCommentsModel
{
public IList<Comment> Comments { get; set; }
public int? ParentId { get; set; }
}
И переключите вид на использование:
@model ShowCommentsModel