2

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?

2
  • If ModelState.Clear() is called before redirecting to your "get" view and that didn't help then you should double check your code as that just work. Commented Jun 5, 2011 at 19:09
  • I see and checked my code. But @Html.DropDownList("asd" + Guid.NewGuid(), Model.Voting.Result.ToSelectList()) works fine. So the problem might be in the method @Html.DropDownListFor Commented Jun 5, 2011 at 20:03

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

The 3rd parameter is 'object htmlAttributes' or 'string optionLabel'. I've tried to pass the value as the3rd param and this hasn't helped me.
making this change added one more item to the result <select> element. This item is selected. So one item is duplicated in the result ddl :(
I do use this constructor with 3rd argument specified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.