Why is the object I tried to get from QueryString empty?

I am trying to create a blog site with asp.net core 3.1. I want to add a comment to the blog post, but I cannot get a query string to which blog post I posted.

I shared my codes as follows. Now, what is the reason why the id from the query string always returns 0?

Or what is the situation I am constantly doing wrong?

There is probably a very simple solution but it took me all day please can you help?

Entity;

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; }
}

Controller :

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");
    }
}

View Page :

<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>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You are trying to get the ‘id’ from the query string of the request. This would require your post URL to look something like: https://example.com/BlogContent/CommentAdd?id=42. Based on the HTML you have provided, I bet your URL looks like: https://example.com/BlogContent/CommentAdd (you can determine what your URL looks like by using your browsers inspection tools to look at the form tag in the HTML). In order to get the desired behavior you will need to update your form tag to look something like this:

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

the important part in this is the addition of the ‘asp-route-id’ tag helper. You can find information about this tag helper (listed as ‘asp-route-{value}’) and others at:

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

But wait, there’s more! In this example I just gave more than likely your URL will turn out to be https://example.com/BlogContent/CommentAdd/42 (I say more than likely because this is determined by your route configuration). This will not give you the expected result and the id will again be zero! So you have a couple of options. The easiest is to change the ‘asp-route-{value}’ tag helper to be something like ‘asp-route-contentId’. This would give you html that looks like:

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

which would generate a URL that would be https://example.com/BlogContent/CommentAdd?contentId=42. You would then need to update your controller code to retrieve the id to look like this:

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

There are a myriad of other options we could go into but I will stop this essay for the time being here.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x