MVC3 DropDownListFor 在回发操作中设置时选定的值不会更改

发布于 2024-12-18 06:13:08 字数 921 浏览 4 评论 0原文

我在这里读过很多关于下拉选择值问题的帖子(不显示等...),但我的问题是相反的。

我希望在按钮通过控制器操作提交页面后返回视图后,下拉菜单始终重置。那么我如何构建它所有的工作,但是否可以每次重置下拉列表?我找不到办法,我已经尝试了很多方法,相信我。

我的观点:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓬勃野心 2024-12-25 06:13:08

这里的根本问题是,当 MVC 在回发时重新绑定表单时,它不是使用 Model,而是使用 ModelState,因此您可以根据需要更改模型,但只会使用绑定的 ModelState。您是否在 ModelState 上尝试过这些方法?

 public bool Remove(string key);
 public void SetModelValue(string key, ValueProviderResult value);

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?

 public bool Remove(string key);
 public void SetModelValue(string key, ValueProviderResult value);
非要怀念 2024-12-25 06:13:08

在回发操作中,您可以根据需要处理当前值,或者在模型中设置 id 并调用视图;

[HttpPost]
public ActionResult Index(ModelClass viewModel)
{
  // Process Value

  viewModel.PspSourceModel.PayAccount.PaymentProviderId = 6;
  return View("Index", viewModel);
}

或者您可以在 HttpGet 上设置默认值,并在帖子中调用 RedirectToAction 结果。

[HttpGet]
public ActionResult Index()
{
  // Set the default values; the following is a rough example, and won't work.
  var viewModel = new ModelClass
  {
    PspSourceModel.PayAccount.PaymentProviderId = 6
  }

  return View("Index", viewModel);
}

[HttpPost]
public ActionResult Index(ModelClass viewModel)
{
  // Process Value

  return RedirectToAction("Index");
}

我希望这是清楚的,如果您需要进一步的帮助,请告诉我。

马特

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;

[HttpPost]
public ActionResult Index(ModelClass viewModel)
{
  // Process Value

  viewModel.PspSourceModel.PayAccount.PaymentProviderId = 6;
  return View("Index", viewModel);
}

Or you could set the default values on your HttpGet, and call a RedirectToAction result in your post.

[HttpGet]
public ActionResult Index()
{
  // Set the default values; the following is a rough example, and won't work.
  var viewModel = new ModelClass
  {
    PspSourceModel.PayAccount.PaymentProviderId = 6
  }

  return View("Index", viewModel);
}

[HttpPost]
public ActionResult Index(ModelClass viewModel)
{
  // Process Value

  return RedirectToAction("Index");
}

I hope this is clear, if you need further assistance, please let me know.

Matt

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文