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).