开发者

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

开发者 https://www.devze.com 2023-03-10 18:19 出处:网络
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

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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消