开发者

Adding to a dropdown in MVC3

开发者 https://www.devze.com 2023-03-12 01:40 出处:网络
Problem: How elegantly add in more cours开发者_如何学Pythones in a drop down in MVC3 I\'m using a seperate screen, however ideally would like to have it all on the same screen, probably using ajax, a

Problem: How elegantly add in more cours开发者_如何学Pythones in a drop down in MVC3

I'm using a seperate screen, however ideally would like to have it all on the same screen, probably using ajax, and jquery.

Adding to a dropdown in MVC3

Adding to a dropdown in MVC3


I ended up just putting on another submit button for my 'child' item and calling it like in the 2nd answer here:

How do you handle multiple submit buttons in ASP.NET MVC Framework?


I have found a better solution is to use the jquery.ui autocomplete. You can then easily add courses. I have used it before very succesfully in similar scenarios. You also get the advantage that you can make ajax calls for big lists.

Here is some sample jquery code that fetches a list from the server based on what the user types in a textbox (routeList is the box that will show the list of courses, routeId is a hidden field that contains the id of the selected route):

$("#routeList").autocomplete({
    source: function (request, response) {
        $("#ajax_loader").show();  //Gets the whirly graphic going
        $("#routeId").val("0");
        var postData = { term: request.term };

        var jqxhr = $.post("/Ajax/RouteAutoComplete", $.postify(postData), function (data) {
            $("#ajax_loader").hide();
            response(data);
        });
    }
    ,
    minLength: 1,
    select: function (event, ui) {
        $("#routeId").val(ui.item ? ui.item.id : "0");
    }
});

When you post back, if routeId=0, then you can add a new course based on the text in routeList

0

精彩评论

暂无评论...
验证码 换一张
取 消