Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a very strange issue which I have batted around for 2 or 3 days. I'm using a Net Core 6.0 MVC web app.

In my view model I have a property like this:

public class ConfirmCodeViewModel
{
...
public long NewUserId { get; set; }
...
}


The model is populated, is passed into a view and this value is preserved in a hidden input within a form, all very standard stuff:


@model ConfirmCodeViewModel
...
<form asp-action="ConfirmCode" method="post">
...
<input type="hidden" asp-for="NewUserId" />
...
</form>


When the form is POSTed then NewUserId comes back as zero, there is no entry for it in the form data posted and thus it is not bound to the incoming model.

If I change the property name to UserId

public long UserId { get; set; }


Then it binds and returns correctly.

The model is being received in the action thusly:

public async Task<IActionResult> ConfirmCode(ConfirmCodeViewModel inputModel)


All other model propertied bind correctly.

This resolves the problem but I would still like to find out just why it does not work with the property named as NewUserId.

Many thanks for any input.

P.S. Apologies if the formatting of code is a bit wonky, my incompetence!

What I have tried:

I have tried changing the type to a string

public string NewUserId { get; set; }


But that did not work either.

I have inspected the incoming Request.Form collection and there is no item called NewUserId returned.

I have checked the view to ensure there is no duplicate element with the same Id.

I have verified (using JS) that the value IS set within the view.

The POST is not being intercepted in any way (e.g. by JS).
Posted
Updated 18-Mar-24 0:15am
v4
Comments
Richard Deeming 18-Mar-24 7:51am    
I suspect the data is being bound from the wrong source - for example, a route segment called NewUserId, rather than the posted form.

Try adding the [FromForm] attribute to the property to force it to always bind to the posted form.

If that doesn't work, use your browser's developer tools to examine the full network request when you submit the form to see if it contains any clues.

1 solution

To my mind, the most obvious thing to investigate is the name of the property that is expected at the server side of the POST call. Verify whether or not it is called NewUserId. I suspect that it's actually called UserId which you are picking up when you send the value across. If the property is called UserId, and you still want to use NewUserId locally, you could always decorate the property with the name that you expect at the other side (this example uses NewtonSoft).
C#
[JsonProperty("UserId")]
public long NewUserid { get; set; }
 
Share this answer
 
Comments
Private Dobbs 19-Mar-24 8:24am    
Thank you both for your input, really appreciated. In the end it was code error causing the issue, not model binding after all. When the model for the final view is generated there are 2 states to be considered. The alternative state was not being handled properly and the model was being initialised with the wrong value - doh!
Pete O'Hanlon 19-Mar-24 8:50am    
I'm glad you got it sorted.

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