为什么我尝试从QueryString获取的对象为空?

问题描述

我正在尝试使用asp.net core 3.1创建一个博客站点。我想在博客文章添加评论,但是我无法获得我在其中发布博客文章查询字符串。

分享了我的代码,如下所示。现在,查询字符串的ID总是返回0的原因是什么?

或者我一直在做错什么情况?

可能有一个非常简单的解决方案,但是花了我整天的时间,请您能帮忙吗?

实体;

public class Content : IEntity
{
    public int Id { get; set; }
    public int CategoryId { get; set; }
    public int UserId { get; set; }
    public virtual List<ContentComment> ContentComments { get; set; }
}

public class ContentComment : IEntity
{
    public int id { get; set; }
    public int UserId { get; set; }
    public int ContentId { get; set; }
    public string Comment { get; set; }
    public DateTime CommnetCreateTime{ get; set; }
    
    public virtual Content Contents { get; set; }
}

控制器:

public class BlogContentController : Controller
{
    private IBlogService _blogService;
    private IContentCommentsServices _contentCommentsServices;

    public BlogContentController(IBlogService blogService,IContentCommentsServices contentCommentsServices)
    {
        _blogService = blogService;
        _contentCommentsServices = contentCommentsServices;
    }

    public IActionResult Index(int id)
    {
        var blogModel = new Contentviewmodel
        {
            Blogs = _blogService.GetById(id)
        };

        return View(blogModel);
    }

    public IActionResult CommentAdd()
    {
        var model = new ContentCommendviewmodel()
        {
            ContentComments = new List<ContentComment>()
        };

        return Ok();
    }

    [HttpPost]
    public IActionResult CommentAdd(ContentComment contentComment)
    {
        contentComment.ContentId = Convert.ToInt32(HttpContext.Request.Query["id"]);
        _contentCommentsServices.Add(contentComment);
        return RedirectToAction("CommentAdd");
    }
}

查看页面

<div class="media-body mt-2" id="yorumyap">
    <h5>Yorum Bırakın</h5>
    <form asp-controller="BlogContent" asp-action="CommentAdd">

        <div class="form-group">
            <textarea name="Comment" class="form-control form-control-sm" rows="3" style="resize: none;" id="commenttext"></textarea>
        </div>
        <div class="text-right">
            <button type="submit" class="btn btn-tekisimbilsim mb-2" id="commendAdd">Yorum Yap</button>
        </div>
    </form>
</div>

解决方法

您正在尝试从请求的查询字符串中获取“ id”。这将要求您的帖子URL类似于:https://example.com/BlogContent/CommentAdd?id=42。根据您提供的HTML,我敢打赌您的URL看起来像:https://example.com/BlogContent/CommentAdd(您可以使用浏览器检查工具查看HTML中的表单标签来确定URL的样子)。为了获得所需的行为,您将需要更新表单标签,使其看起来像这样:

<form asp-controller="BlogContent" asp-action="CommentAdd" asp-route-id="@Model.BlogId">

其中的重要部分是添加了“ asp-route-id”标签帮助程序。您可以在以下位置找到有关此标签助手的信息(列为“ asp-route- {value}”)和其他信息:

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-3.1#the-form-tag-helper

但是,等等,还有更多!在此示例中,我给出的URL可能是https://example.com/BlogContent/CommentAdd/42的可能性要大得多(我说的可能性更大,因为这是由您的路由配置决定的)。这不会给您预期的结果,并且ID将再次为零!因此,您有两种选择。最简单的方法是将“ asp-route- {value}”标记帮助程序更改为“ asp-route-contentId”。这将使您的html看起来像这样:

<form asp-controller="BlogContent" asp-action="CommentAdd" asp-route-contentId="@Model.BlogId">

,它将生成将为https://example.com/BlogContent/CommentAdd?contentId=42的URL。然后,您需要更新控制器代码以检索ID,如下所示:

contentComment.ContentId = Convert.ToInt32(HttpContext.Request.Query["contentId"]);

我们还有很多其他选择,但我暂时不会在此发表。