Serhiy
Serhiy

Reputation: 4507

change the value of dropdown list after form submition ASP.NET MVC

I have a very strange problem. I want to change selected value of the dropdownlist after form submition. I know that HtmlHelper is retrieving the ModelState value, which is filled with the posted data. But I have a redirect from my POST action to GET action! However my ddl is populated with value submitted during the post. I've also added this code to my post action: ModelState.Clear(), but this hasn't helped me neither.

I've added another ddl to my form just for debugging;

   @Html.DropDownList("asd" + Guid.NewGuid(), Model.Voting.Result.ToSelectList())

It always appears with value provided by the server code. But the target ddl

@Html.DropDownListFor(x => x.Voting.Result, Model.Voting.Result.ToSelectList())

always has a value posted by user. How can I populate the target ddl?

Upvotes: 2

Views: 1517

Answers (1)

Eranga
Eranga

Reputation: 32437

You have to pass the initial value to it like

@Html.DropDownListFor(x => x.Voting.Result, 
new SelectList(Model.Voting.Result, "Id", "Name", /*initial value*/))

Use this constructor of SelectList class

Edit initially I have put the argument on wrong method

Upvotes: 1

Related Questions