0

I have a ViewModel which contains Different Type of List

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.

How Can I Do that?

Any suggestions.

Thanks,

1 Answer 1

1

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.

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.

Here's an example:

HTML:

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

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.

Sign up to request clarification or add additional context in comments.

4 Comments

You Mean display all data at first and hide the unwanted data? and use hide/show in jquery?
@ArijitMukherjee, That would be the only way without calling your controller, but why do you not want to do that?
I've edited and added a small example. I'm not sure this is exactly what you wanted but it will help you understand how you should do this.
The problem is when I Use ajax, then I value does not persist from one view to another for more than once. one time it can be achieved using TempData.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.