AJAX Cascading with MVC4

后端 未结 2 1328
故里飘歌
故里飘歌 2021-01-16 07:35

I used the below method for doing Async postback using AJAX. This works fine on clicking submit. But i would like to know, is that pos

相关标签:
2条回答
  • 2021-01-16 08:24

    The answer to your first question "is that possible to call various ActionMethods in a controller via AJAX" is a big yes. You may call any action method from your controller through Ajax though the only result generated depends on various things like whether you send a view or partial view or JSON result.

    for your next question :

    I will be posting some codes

    Controller.cs

    public JsonResult getCity(string country)
        {
            var temp = (from cntry in db.Table3.OrderBy(s => s.country)
                        where (string.Compare(cntry.country, country) == 0)
                        select cntry.city).ToList();
            return Json(temp, JsonRequestBehavior.AllowGet);
        }
    

    View

    <h1>
    Countries</h1>
    <select name="countries" class="combo">
    <option value=""></option>
    
    @{
        foreach (var t in (List<string>)ViewBag.countries)
        {
        <option value=@t>@t</option>
        }
    }
     </select>
    <h1>
    State</h1>
    <select name="city" class="combo2">
    </select>
    <div id="tese">
    </div>
    @*
    The following jquery code finds the selected option from country dropdown 
    and then sends an ajax call to the Home/getcity method 
    and finally populate it to the city dropdown 
    *@
    <script type="text/javascript">
    $('body').on('change', '.combo', function () {
        var selectedValue = $(this).val();
        alert(selectedValue);
        $.get("/Home/getcity", { country: selectedValue }, function (data) {
            $("#tese").html(data);
            $(".combo2").html("<option value = \"\"></option>")
            $.each(data, function (index, value) {
                $(".combo2").append("<option value = \"" + value + "\">" + value + "</option>");
            });
            $(".combo2").html()
        });
    });
    </script>
    

    This will show a dropdown of countries list. Once a country is selected it will render a new dropdown of city list

    0 讨论(0)
  • 2021-01-16 08:33
    public JsonResult getCity(string country)
        {
            var temp = (from cntry in db.Table3.OrderBy(s => s.country)
                        where (string.Compare(cntry.country, country) == 0)
                        select cntry.city).ToList();
            return Json(temp, JsonRequestBehavior.AllowGet);
        }
    

    View

    <h1>
    Countries</h1>
    <select name="countries" class="combo">
    <option value=""></option>
    
    @{
        foreach (var t in (List<string>)ViewBag.countries)
        {
        <option value=@t>@t</option>
        }
    }
     </select>
    <h1>
    State</h1>
    <select name="city" class="combo2">
    </select>
    <div id="tese">
    </div>
    @*
    The following jquery code finds the selected option from country dropdown 
    and then sends an ajax call to the Home/getcity method 
    and finally populate it to the city dropdown 
    *@
    <script type="text/javascript">
    $('body').on('change', '.combo', function () {
        var selectedValue = $(this).val();
        alert(selectedValue);
        $.get("/Home/getcity", { country: selectedValue }, function (data) {
            $("#tese").html(data);
            $(".combo2").html("<option value = \"\"></option>")
            $.each(data, function (index, value) {
                $(".combo2").append("<option value = \"" + value + "\">" + value + "</option>");
            });
            $(".combo2").html()
        });
    });
    </script>
    
    0 讨论(0)
提交回复
热议问题