Ok, so finally worked out what the issue was. The request was a Get request and there was a query string value called 'COESNo'. This seem to confuse the Hiddenfor. I changed the variable name to COESValue and it works a treat. Is it trying to model bind in the razor view?
I am having trouble with a html helper, why is it not working?
-
01-07-2022 - |
Question
I have a viewModel that has a field called COESNo..
when I try to write it in the view like so
@Html.HiddenFor(m=>m.COESNo)
and then try to access it later using Jquery $("#COESNo).val() it says it is empty
but if I do the following
<input type="hidden" id="COESNo" value="@Model.COESNo" />
I can get the value from Jquery.. why is it setting the value to empty when I use the hiddenfor?
Thanks
Solution 2
OTHER TIPS
Inspect the source of the page, specifically the id attribute of the hidden field generated by @Html.HiddenFor(m=>m.COESNo). If this line is inside of a partial view (that is, the partial's model is a subset of the main model), or is inside of a loop, the id attribute's value will have a prefix. You will then need to take this prefix into account in your jQuery selector.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow