CodesTips

Loading

black laptop computer turned on on table

how to keep dropdownlist selected value after button click postback in mvc

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.

resetdropdownlistvalue

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;

buttonclickcodeSolution:

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:

Ddmodel

ApcpaymentstatusClickactionmethod

bindmodelindropdownlist

Model Class:

dropdownmodelclass

Controller:

dropdownlistbindmethod

Read More