Introduction:
In this blog we will look into an another result to the problem with dropdownlist where selecteditems aren’t retained the value after button click on postback in mvc.
Problem Description:
When we select an item in dropdownlist and do click a button then the value in dropdownlist will reset to the first item in dropdownlist.
Generally this type of problems happens when we reload the dropdownlist on every post method . To resolve the problem we will load the dropdownlist value on button click and then select the selected value is to be true. Below code we use foreach loop and selectlistitem property item.selected=true;
Solution:
Upon closer examination, I observed that during the dropdownlist population, the user is assigning an empty string to the “value” property. Consequently, the dropdownlist renders on the page at runtime without a populated value, leading to this issue caused by the invalid code for the dropdownlist.
View:
Model Class:
Controller:
Button Click on Controller Level
Final Output:
I’m extremely impressed with your writing skills as well as with the layout on your blog.
Is this a paid theme or did you modify it
yourself? Anyway keep up the excellent quality writing, it’s rare
to see a nice blog like this one these days.
Here is my website John E. Snyder