简体   繁体   English

恢复组合框中的所选值

[英]Restore selected value in combobox

I have some comboboxes on form, like this: 我在表单上有一些组合框,像这样:

@Html.DropDownListFor(x => x.ComboId, Model.ComboValues)

Also, I have simple action method, which returns recieved model in same view. 另外,我有简单的操作方法,该方法在同一视图中返回收到的模型。 When change selected item, and submit form, I'll get same page with correct selected value in combobox. 当更改选定的项目并提交表单时,我将在组合框中看到具有正确选定值的同一页面。 But when I click "Back" button in my browser (check this in Chrome 25+, IE 9+), I'll get last selected value. 但是,当我在浏览器中单击“返回”按钮时(在Chrome 25 +,IE 9+中选中此按钮),我将获得上次选择的值。

For example , let ComboValues contains 2 values: 1 and 2 . 例如 ,让ComboValues包含2个值: 12 Previously, ComboId = 1 . 以前, ComboId = 1 When I change it to 2 and submit, I'll have form with correct value 2 . 当我将其更改为2并提交时,我将获得具有正确值2表格。 But when I click "Back" button in browser, I'll still have combobox with value 2 (but I was hope to see 1 ). 但是,当我在浏览器中单击“返回”按钮时,我仍然会看到带有值为2组合框(但希望看到1 )。

This behavior occurs, because I don't call action, when "Back" in browser. 在浏览器中单击“返回”时,会发生这种现象,因为我没有调用操作。 And my page wasn't redrwaing. 而且我的页面没有重新编排。 We can fix this behavior with recalcaluting combobox selected item when page loaded: 我们可以通过在页面加载时重新计算组合框所选项目来解决此问题:

$(function () {
    $("#@Html.IdFor(m => m.ComboId)").val("@Model.ComboId");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM