简体   繁体   English

在不使用Ajax或MVC中提交的情况下刷新视图/局部视图

[英]Refresh A View/Partial View without using Ajax or Submit in MVC

I have a ViewModel which contains Different Type of List 我有一个包含不同类型的列表的ViewModel

When first time page is loaded, I wanted to display the List of Data against the drop-down selected. 首次加载页面时,我想针对所选下拉列表显示“数据列表”。

When the Drop-Down Changes then I want to display List of Data specific to that type. 当下拉列表更改时,我要显示特定于该类型的数据列表。

But I don't want to go to my Controller, as in that Case I need to do the search/query, where as I already have all the types in my View at first. 但是我不想去我的控制器,因为在那种情况下,我需要进行搜索/查询,因为我一开始已经在View中拥有所有类型。

How Can I Do that? 我怎样才能做到这一点?

Any suggestions. 有什么建议么。

Thanks, 谢谢,

If you dont want to go to the server(ajax\\submit) then you will need to download all the data(in the first response) and change the list using javascript. 如果您不想转到服务器(ajax \\ submit),则需要下载所有数据(在第一个响应中)并使用javascript更改列表。

When the select box's change occurs, you javascript will need to get the selected value and update the new list to work against it. 当选择框发生更改时,您的JavaScript将需要获取所选值并更新新列表以对其进行处理。

Here's an example: 这是一个例子:

HTML: HTML:

<select id="s1"></select>
<select id="s2"></select>

JS: JS:

var currData = ["1", "2", "3"];
var otherData = [
    ["a", "b", "c"],
    ["d", "e", "f"],
    ["g", "h", "i"]
]


for (var i = 0; i < currData.length; i++) {
    var option = $('<option value="' + currData[i] + '">' + currData[i] + '</option>');
    $("#s1").append(option);
}
// s1 and s2 are the same when the page loads.
$('#s2').html($('#s1').html());

$('#s1').change(function () {
    var idx = currData.indexOf($(this).val());
    var newSelectData = otherData[idx]; // change s2 due to s1's selection
    $('#s2').children().remove(); // clear the s2 select box
    for (var i = 0; i < newSelectData.length; i++) {
        var option = $('<option value="' + newSelectData[i] + '">' + newSelectData[i] + '</option>');
        $("#s2").append(option);
    }
});

JSFIDDLE . JSFIDDLE

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

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