AJAX call on OnChange event in MVC

前端 未结 5 1052
温柔的废话
温柔的废话 2021-01-02 19:39

I have to make an AJAX call on the onchange event of a dropdownlist which is part of a view. On the change event i need to call the database, do some calculations display th

5条回答
  •  青春惊慌失措
    2021-01-02 20:10

    Ok, if what you want ia a way to call a onchange event when a dropdownlist change this can be helpful:

    @Html.DropDownListFor(
                            model => model.SelectedId,
                            new SelectList(ViewBag.Ids, "Id", "Name"),
                            "[All]",
                            new { onchange = "onChangeId();", @id ="municipalityDropDown" }
                            )
    

    then you can have this javascript code and you ajax code. And here is an example of a jax code to call a action method.

    function onChangeId() {
          jQuery.ajax({
                url: '@Url.Action("Action Method Name", "Controller Name")',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({ data: data2 }),
                success: function (result) { }
            });  
        }
    

提交回复
热议问题