0

I have cascading dropdowns in my MVC partial view. DD1 drives the values in DD2. When I select DD1 I want DD2 to be populated with, based on a DB table, the correct values based on DD1's value.

My thought was to make DD2 a partial view and nest in my form. Then, with ajax, I can tell the partial view to refresh and pass it the value of DD1.

The problem is, when I submit the whole view (with both DD1 and DD2 and a bucnh of other stuff, how do I get the value that is in DD2?

I'm trying to solve this problem using MVC, rather than triggering a javascript function on change of DD1 to make a JSON call to get the options and then using javascript to modify DD2 to the correct values.

How should I do this?

4

2 回答 2

0

How big are your value sets for each drop down?

I was attempting to do this same thing a few years ago. DD1 was United States and Canada, and DD2 was the associated States and Provinces. If your data set is relatively small, you're better off just putting all the select list markup for both cases in the page, and then swapping it out with javascript (jQuery). You'll be saving yourself the request round trip, regardless of whether you go ajax or a full page refresh.

If the data set is large and it doesn't make sense to put all values in the markup, and you want to use MVC views instead of modifying the DOM with an ajax call, then just refresh the entire page. If you want to go ajax, then just modify the DOM with jQuery; you don't need a partial view to accomplish this.

于 2013-06-11T15:16:43.580 回答
0

除非您想进行整个页面的回发,否则您将不得不使用 javascript。对于这种类型的事情,javascript/ajax 是要走的路。当我切换到 MVC 时,我个人很难接受所有这些业务逻辑都发生在 MVC 模型之外。但归根结底,它使网站运行得最好(用户看不到您的代码并且知道它有多漂亮)。

无论如何,除非您发布整个页面,否则部分将不起作用,因为不使用 javascript,部分将呈现为该页面/表单的一部分。

我只想在第一个下拉列表中添加一个 onchange 事件,触发对同一控制器中的方法的 json 调用......类似于

...jQuery ...

$("#mydropdown").change(function() {
    $.post("/Controller/DropdownChangedJSON", { firstdropdownvalue: $("#mydropdown").val() }, function(data) {
    $("#seconddropdown").empty();
    //    loop through "data" to populate dropdown
}); //post
}); //mydropdown_change()

在你的控制器中:

public JsonResult DropdownChangedJSON(string firstdropdownvalue) {
    //get results
    List<datamodel> myarray = //something
    return new JsonResult { Data = new { success = true, rows = myarray } };
}

希望这可以帮助

于 2013-06-11T15:21:32.773 回答