How to receive an array in AJAX send from the ASP .NET controller?

145 Views Asked by At

The tecnologies are:

  1. ASP .NET MVC2
  2. JQuery v3

My code is as follows:

Controller:

public ActionResult Create()
    {
        ViewData["department"] = new SelectList(_db.department.ToList(), "idDepartment", "department", 1);
        ViewData["municipality"] = new SelectList(_db.municipality.Where(m => m.idDepartment == 1).ToList(), "idMunicipality", "municipality", 1);
        return View();
    }

    [HttpPost]
    public JsonResult Ajaxmuni(int? Id)
    {
        var data = _db.municipality.Where(m => m.idDepartment == Id).ToList();
        return this.Json(data);
    }

View (front-end)

<script type="text/javascript">
$(document).ready(function () {
    $('#department').change(function () {
        var $idDepart = $('#department').val();
        $.ajax({
            url: "/Filegeneral/Ajaxmuni",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            cache: false,
            data: { Id: $idDepart},
            success: function (data) {
                alert("data: " + data);
            },
            error: function (xhr) {
                alert("Error" + xhr);
            }
       });
    });
});
</script>
<label for="department">Select deparment</label>
<%: Html.DropDownList("department", null, new { @class = "form-control"})%>                        

<label for="municipality">Select municipality</label>
<%: Html.DropDownList("municipality", null, new { @class = "form-control"})%>

The action Ajaxmuni does not receive the value that I sent from ajax, if I change to GET I will receive it but I do not want it to be by GET but by POST. Now when I do it for GET I receive the department Id but I can not receive the list in the client's view. how do i solve this.

UPDATE I managed to solve the part of which I could not receive the value of the Id in the action, doing in this way:

<script type="text/javascript">
    $(document).ready(function () {
        $('#department').change(function () {
            var $idDepart = $('#department').val();
                $.ajax({
                    url: "/Filegeneral/Ajaxmuni",
                    type: "POST",
                    cache: false,
                    data: { Id: $idDepart},
                    success: function (data) {
                        alert("data: " + data);
                    },
                    error: function (xhr) {
                        alert("Error" + xhr);
                    }
                });
        });
    });

In the controller:

[HttpPost]
public JsonResult Ajaxmuni(int? Id)
{
    var data = _db.municipality.Where(m => m.idDepartment == Id).Select(m => new{ m.idMunicipality, m.municipality});
    return this.Json(data);
}

now I do not know how to extract the data in the view, the method

alert("data:" + data);

month shows the following:

enter image description here

how do I extract the data?

0

There are 0 best solutions below