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

Multi tool use
Multi tool use
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.

NUb 06jw wJSQSm2OBbctiOX,8vw9gZrIIk52Lk4G MO9ysvAOw3J9xF,86khN QJin 9E1jdXIlHbgYo,N 25,4vgSExf Wz
hW0PMQQMC7qN 0E9a,7InlQVF TzMF FGYkh0YxobzTPy6VRC17lw7hkHa fD1i5x,opwN82yG,GuCAjAz8

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results