我正在尝试将整数id传递给局部视图;我正在建立评论系统,其中使用可以评论帖子。这就是我正在做的事情
我正在使用 ASP.NET MVC Core
Post.cshtml
@model MyApp.Models.Post
@Html.Partial("ShowComments","Home", new MyApp.Models.Comment { PostId = Model.PostId})
HomeController.cs
public IActionResult ShowComments (int PostId)
{
-- Get comment from database based on PostId
return(comments)
}
Comment.cs
public class Comment
{
public int CommentId {get; set;}
public int PostId {get; set;}
}
ShowComments.cshtml
@model IEnumerable<MyApp.Models.Comment>
@foreach (var item in Model)
{
@Model.CommentText
}
以下是我收到的错误消息
无法从'MyApp.Models.Comment'转换为 'Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary'
我想将PostId从父视图传递到局部视图,以便部分视图可以加载与该帖子相关的评论数据。
答案 0 :(得分:0)
参考,
https://docs.microsoft.com/en-us/aspnet/core/mvc/views/partial
您可以使用ViewComponent或将其注释作为主视图模型的属性之一,并将其传递给部分视图,如。
@Html.Partial("ShowComments",Model.Comments)