1
votes

I'm trying to enable posting Comments from the view where I display a Forum Post by id. That's the model which my view receives:

    @model PostViewModel

the Model has and "Id" property, which I send to a controller with javascript:

    <script>
    $("#target").click(function () {
        $("#formPlace").load('@Url.Action("AddComment","Posts", new { postId = Model.Id })');            
    });
    </script>

That's the controller action I send the "postId" to:

    [HttpGet]
    public ActionResult AddComment(int postId)
    {
        var comment = new CommentViewModel();
        comment.PostId = postId;
        return this.PartialView("_AddComment", comment);
    }

This view returns a form in which the user has to fill the comment's content:

    @model MvcTemplate.Web.ViewModels.Posts.CommentViewModel
    <div>
    @using (Html.BeginForm("AddComment", "Posts", FormMethod.Post))
    {
    <div>Enter your comment here:</div>
    <div>
        @Html.TextAreaFor(x => Model.Content)
    </div>
    <input type="submit" name="Submit" />
    }
    </div>

When the view receives the model its "PostId" is still correct = what I set it with the javascript. However after the user submits the form, the CommentViewModel which is sent to the controller has 0(default int value) for "PostId". This is the controller action:

    [HttpPost]
    public ActionResult AddComment(CommentViewModel viewModel)
    {
        // transfer the view model to db model and save;
    }

Any idea how I can keep the correct PostId?

3
Can I see the code for CommentViewModel please?CodingYoshi

3 Answers

2
votes

When you submit the form to the AddComment action method, the default model binder will try to bind the form field values to the properties of your CommentViewModel object. Your form has an input field for the Content property, but you do not have one for the PostId. So the browser will send the value of only form element with name Content.

If you want PostId, You need to keep the Post id value in the form. Since user does not need to see/edit this, you may keep this in a hidden input field in the form.

You can use the Html.HiddenFor helper method to generate the hidden field with the name and value for the corresponding property.

@using (Html.BeginForm("AddComment", "Posts", FormMethod.Post))
{
  <div>Enter your comment here:</div>
  <div>
    @Html.TextAreaFor(x => Model.Content)
  </div>
  @Html.HiddenFor(s=>s.PostId)
  <input type="submit" name="Submit" />
}

Or just a hidden input element markup (the helper ultimately generate this only)

<input type="hidden" name="PostId" value="@Model.PostId" />
0
votes

You need to put hidden field with PostId value in your form. You are only posting content. Alternatively your post action AddComment should have url parameter postId, so form's action url will include postId.

0
votes

You can set your PostId in a TempData and can get in subsequent post action in controller. If you want to get it from view you need to set it in a hidden field as suggested by Shyju.