Search code examples
c#asp.net-coreasp.net-core-mvc

Asp.Net Issue Passing form values to Post method in controller


In my ASP.Net app i have a blogView Model which has a blog inside it and a AddComment model inside it like this:

  public class BlogViewModel
    {
        public Blog blog { set; get; }
        public AddCommentViewModel addComment { set; get; }
    }

in my controller i am fetching the blog and passing it to the page like this:

   public IActionResult ViewBlog(int id)
        {
            var blogViewMoodel = new BlogViewModel
            {
                blog = _db.Blogs.Include(b => b.User).FirstOrDefault(u => u.Id == id),
                addComment = new AddCommentViewModel(),
            };
            blogViewMoodel.blog.Comments = _db.Comments.Where(u=>u.BlogId == blogViewMoodel.blog.Id).ToList();

            if (blogViewMoodel.blog == null)
            {
                return RedirectToAction("Index");
            }
            return View(blogViewMoodel);
        }

Now within my view i am displaying the blog and then the form to add a comment like this:

//...Blog content above
<div class="container mt-5">
    <h3>Comments</h3>
    @if (SignInManager.IsSignedIn(User))
    {
        <form method="post">
            @Html.AntiForgeryToken()
            <input asp-for="addComment.UserId" value="@UserManager.GetUserId(this.User)" hidden/>
            <input asp-for="addComment.BlogId" value="@Model.blog.Id" hidden/>
            <div class="row">
                <div class="col-8">
                    <input asp-for="addComment.Content" class="form-control border" placeholder="Add a comment..."/>
                </div>
                <div class="col-4">
                    <button type="submit" class="btn btn-primary">Submit</button>
                </div>
            </div>
        </form>
    }
    @if (Model.blog.Comments.Count == 0)
    {
        <p class="mt-5">There are no comments. Be the First the Comment!</p>
    }
    else
    {
        @foreach (var comment in Model.blog.Comments)
        {
            <p>@comment.Content</p>
        }
    }
</div>

When my form is submitted and i try and add the comment to the Db i cant because al the values in the AddCommentViewModel are either 0 or null does anyone know why? Thanks. This is my Post Method:

        [HttpPost]
        public IActionResult ViewBlog(AddCommentViewModel commentModel)
        {
            if (ModelState.IsValid)
            {
                var comment = new Comment
                {
                    Content = commentModel.Content,
                    CreatedAt = DateTime.Now,
                    UserId = commentModel.UserId,
                    BlogId = commentModel.BlogId,
                };
                _db.Comments.Add(comment);
                _db.SaveChanges();
            }
            return View();
        }

When i debug this and see the variables in the commentModel is all null or 0.


Solution

  • When posting data through a form the content will be sent as contentType multipart/form-data. From the controller level, you need to tell in which content the incoming request data will be in. In your case, the FromForm attribute will help you here.

    Try adding it to the controller:

    [HttpPost]
    public IActionResult ViewBlog([FromForm]AddCommentViewModel commentModel)