Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 quarystring to which blog post I posted.

I shared my codes as follows. Now, what is the reason why the id from the quarystring 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;
C#
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 :

C#
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 :

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


What I have tried:

I need a block of code that can dynamically comment. Or please tell me what I am missing in this code.

Please help me.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900