简体   繁体   中英

Calling specific method (not an Action) from view MVC Asp.net on on-change event of html.dropdownlist

In my ASP.NET MVC project, I have list of data saved in ViewBag , I am populating a drop-down from that where parent id null, then selecting a value from that drop down and which select data which contains their parent_id of selected value's id and populate another drop-down.

Note: I have stored that data in session.

I don't want to make dual call for fetching data again, so I want to make private method inside controller , and want to call that on on-change event of drop-down and load data from ViewBag

Or anyone can suggest quickest way by doing it with ajax / jquery.

$('#parents').change(function() {
    var el = $(this),
        parent_id = el.val();

    $.getJSON('@Url.Action("GetSomeList", "MyController", new {parent_id = paren_id})', function (data) {
        var ddl = $('#ddlChildDropDown');
        $.each(data, function (i, v) {
          ddl.append($('<option />', { text: v.SomeValue, value: v.SomeID}))
        })
    })
});

then in your controller

public JsonResult GetSomeList(int parent_id)
{
    var repo = new DataBaseReop()
    var my_list = repo.getListData(parent_id); // produces a list with SomeID and SomeValue

    return Json(my_list, JsonRequestBehavior.AllowGet);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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