Home > Blockchain >  Passing model from get method to a post nulls the model
Passing model from get method to a post nulls the model

Time:12-09

When I pass model to the view on the post method the ProductId and UserId get nulled.

        [HttpGet]
        public async Task<IActionResult> AddReview(int id)
        {
            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

            var model = new AddReviewViewModel()
            {
                ProductId = id,
                UserId = userId
            };

            return View(model);
        }

        [HttpPost]
        public async Task<IActionResult> AddReview(AddReviewViewModel addReviewViewModel)
        {
            if (!ModelState.IsValid)
            {
                return View(addReviewViewModel);
            }
            //...
        }

Here is how I call the post method.


<div >
    <div >
        <form asp-action="AddReview" method="post">
            <div >
                <label asp-for="@Model.Comment" >Comment</label>
                <input asp-for="@Model.Comment"  aria-required="true" />
                <span asp-validation-for="Comment" ></span>
            </div>
            <div >
                <label asp-for="@Model.Rating" >Rating</label>
                <input asp-for="@Model.Rating"  aria-required="true" />
                <span asp-validation-for="Rating" ></span>
            </div>
            <div >
                <input  type="submit" value="Submit Review" />
            </div>
        </form>
    </div>
</div>

I have done something like this while adding a new product but I haven't had any problem.

CodePudding user response:

You have to return the data. It needs to make a round-trip. This is typically done with hidden input fields.

<input type="hidden" id="ProductId" name="ProductId" value="@Model.ProductId">
<input type="hidden" id="UserId" name="UserId" value="@Model.UserId">

Full example

<form asp-action="AddReview" method="post">
    <input type="hidden" id="ProductId" name="ProductId" value="@Model.ProductId">
    <input type="hidden" id="UserId" name="UserId" value="@Model.UserId">
    <div >
        <label asp-for="@Model.Comment" >Comment</label>
        <input asp-for="@Model.Comment"  aria-required="true" />
        <span asp-validation-for="Comment" ></span>
    </div>
    <div >
        <label asp-for="@Model.Rating" >Comment</label>
        <input asp-for="@Model.Rating"  aria-required="true" />
        <span asp-validation-for="Rating" ></span>
    </div>
    <div >
        <input  type="submit" value="Submit Review" />
    </div>
</form>

If your version supports taghelpers you can also write:

<input type="hidden" asp-for="ProductId">
<input type="hidden" asp-for="UserId">

Or using the HtmlHelper:

@Html.HiddenFor(m => m.ProductId)
@Html.HiddenFor(m => m.UserId)

In all cases, make sure you add this inside the form.

CodePudding user response:

It sounds like the ProductId and UserId properties are getting nulled in the AddReview method when it is called via a POST request. This could be happening for a few reasons:

  1. The ProductId and UserId properties are not included in the POST request. In this case, you would need to include them in the form that is submitted to the POST method, either as hidden fields or as additional form fields that the user can fill out.
  2. The ProductId and UserId properties are being overwritten by default model binding. In this case, you can try to use the [Bind] attribute on the addReviewViewModel parameter in the POST method to specify which properties should be bound from the request.

This will instruct the model binder to only bind the ProductId and UserId properties from the request and leave any other properties unmodified.

It is also possible that there is some other issue causing the ProductId and UserId properties to be nulled in the POST method. Without more information about your code and the request that is being sent, it is difficult to provide a more specific answer. I hope this helps!

example:

[HttpPost]
public async Task<IActionResult> AddReview([Bind("ProductId", "UserId")] AddReviewViewModel addReviewViewModel)
{
    if (!ModelState.IsValid)
    {
        return View(addReviewViewModel);
    }
    //...
}
  • Related