[英]How to pass value to partial view or controller
我试图将整数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从父视图传递到局部视图,以便局部视图可以加载与该帖子相关的评论数据。
引用这个
https://docs.microsoft.com/zh-cn/aspnet/core/mvc/views/partial
您可以使用ViewComponent或将其注释作为主视图模型的属性之一,然后将其传递到局部视图中。
@Html.Partial("ShowComments",Model.Comments)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.