【问题标题】:jquery multiselect not retaining selection after postbackjquery multiselect在回发后不保留选择
【发布时间】:2015-03-24 18:47:06
【问题描述】:

回发后我遇到了引导多选控制的问题。在我看来,有一个像

这样的下拉菜单
@Html.DropDownListFor(model => model.SelectedReportToRoles, Model.NotificationReportToRoles, new { multiple = "multiple", @class = "select-optional multiselect", @id = "ddlReportTo" })

并且,当我从 ddl 中选择多个项目并将其保存到 DB,然后返回页面时,我只能看到我选择的项目中的第一个项目仅在 ddl 中显示为已选中。如果我刷新页面,它会根据需要运行。即,ddl 将数据库中的所有项目显示为已选择(如选择了 3 个项目)

我的控制器动作是这样的

   [HttpGet]
    public ActionResult Create()
    {
        return this.View(this.GenerateViewModel());
    }

    private static NotificationSettingsViewModel GenerateNotificationSettingsViewModel()
    {
        NotificationSettingsViewModel notificationSettingsViewModel = new NotificationSettingsViewModel();

    // Getting the roles from DB and assigning that to int array selectedReportToRoles

    notificationSettingsViewModel.NotificationReportToRoles = new MultiSelectList(roles, "RoleID", "RoleDescription", selectedReportToRoles);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(NotificationSettingsViewModel notificationSettingsViewModel)
    {
          // Save to Db
          return this.View(this.GenerateViewModel());
    }

我使用的是 jquery 1.8.2 版

感谢任何帮助。

【问题讨论】:

  • 你的post方法在哪里?
  • 我认为这并不重要,因为这些值已发布,并且我能够正确保存到 DB。如果需要,我可以发帖
  • 是的,这很重要,因为在这里发布模型时,它会丢失您的 DropDownList 数据,因此无论何时从 POST 方法返回,它都必须具有该集合,否则它将没有任何价值.
  • 不需要提供最后一个参数 - 您对 selectedReportToRoles 的强绑定,所以无论它包含什么值,都会选择这些值。
  • post 方法中应该是return View(notificationSettingsViewModel)(生成新模型有什么意义?-只需重新分配SelectList

标签: jquery asp.net-mvc bootstrap-multiselect


【解决方案1】:

我通过将更改的参数保存到 TempData 然后从我的发布操作重定向到 Get 操作解决了这个问题。

return this.RedirectToAction("Create");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 2011-01-30
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多