MVC3 DropDownListFor 在回发操作中设置时选定的值不会更改
我在这里读过很多关于下拉选择值问题的帖子(不显示等...),但我的问题是相反的。
我希望在按钮通过控制器操作提交页面后返回视图后,下拉菜单始终重置。那么我如何构建它所有的工作,但是否可以每次重置下拉列表?我找不到办法,我已经尝试了很多方法,相信我。
我的观点:
@Model.PspSourceModel.PayAccount.PaymentProviderId
<br />
@Html.DropDownListFor(
x => x.PspSourceModel.PayAccount.PaymentProviderId,
new SelectList(Model.PspSourceModel.PaymentProviders, "Value", "Text", "-- please select --"),
"-- please select --"
我的控制器:
// I've tried forcing the selected value id here - doesn't effect the dropdownlist still?
pspVM.PspSourceModel.PayAccount.PaymentProviderId = 1;
return (View(pspVM));
我的网页显示:
1 (the id I set in the Action)
dropdownlist with the id=6 or whatever value was chosen prior to submitting the form.
从 SO 和更广泛的网络上的问题和答案中,我认为下拉列表似乎与您选择的 ID 相关联,但我如何覆盖它以每次将下拉列表重置为“请选择”?
提前致谢。
I've read a lot of posts on here regarding the dropdown selected value issues (not showing etc etc...) but mine is the opposite problem.
I want the drop down to always reset after the view is returned after a button submits the page through the controller action. So how I've structured it all works but is it possible to reset the dropdown list each time? I can't find a way to do it and I've tried a lot of ways, believe me.
My View:
@Model.PspSourceModel.PayAccount.PaymentProviderId
<br />
@Html.DropDownListFor(
x => x.PspSourceModel.PayAccount.PaymentProviderId,
new SelectList(Model.PspSourceModel.PaymentProviders, "Value", "Text", "-- please select --"),
"-- please select --"
My Controller:
// I've tried forcing the selected value id here - doesn't effect the dropdownlist still?
pspVM.PspSourceModel.PayAccount.PaymentProviderId = 1;
return (View(pspVM));
My Webpage shows:
1 (the id I set in the Action)
dropdownlist with the id=6 or whatever value was chosen prior to submitting the form.
From the questions and answers on SO and the wider web I thought the dropdownlist seems tied to the id you choose but how do I override that to reset the dropdown to 'please select' each time?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的根本问题是,当 MVC 在回发时重新绑定表单时,它不是使用 Model,而是使用 ModelState,因此您可以根据需要更改模型,但只会使用绑定的 ModelState。您是否在 ModelState 上尝试过这些方法?
The fundamental problem here is that when MVC is rebinding the form on postback, it is not using Model but ModelState, so you can change your model as much as you like but only the bound ModelState will be used. Have you tried either of these methods on ModelState?
在回发操作中,您可以根据需要处理当前值,或者在模型中设置 id 并调用视图;
或者您可以在 HttpGet 上设置默认值,并在帖子中调用 RedirectToAction 结果。
我希望这是清楚的,如果您需要进一步的帮助,请告诉我。
马特
In your action on your postback, you could process the current value as you need to and either set the id in the model and call the view;
Or you could set the default values on your HttpGet, and call a RedirectToAction result in your post.
I hope this is clear, if you need further assistance, please let me know.
Matt