ASP.NET Core 2.1 MVC - Model Binding - Dropdown List Value

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


ASP.NET Core 2.1 MVC - Model Binding - Dropdown List Value



I have a ViewModel something like this which contains two properties.



UserSelectedState - String value



ListOfStateNames - This is a List of SelectListItem.



I am able to bind the model to the View and the user sees the list of state names and makes one selection. I am posting it back to the controller and I am able to see the "UserSelectedState" value assigned correctly.



But, the "ListofStateNames" is empty in the Model when posted back.



Just trying to understand why it should be empty. Since it's the same Model which is populating the list items in the View.



Any explanation on why it is empty ?




1 Answer
1



This is just how html POST works.



On a roundtrip only values of certain types are returned: input, hidden field, selected option. Where readonly fields, labels, etc. are not returned.



Since you know UserSelectedState, you don't need the complete list from the client. In fact, you don't want to use it either, since you cannot trust the client. All you need is the selected value or preferably id.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

'Series' object is not callable Error / Statsmodels illegal variable name