Frage

I'm scratching my head but can't find what I'm missing here. My RegionCoreModel properties always getting null.

Html:

<div>
    <input type="text" id="manager_name" name="manager_name" value="">
    <input type="text" id="no_of_emplyee" name="no_of_emplyee" value="">
    <button id="update">Update</button>
</div>

js

$("#update").on("click", function() {
                var param = JSON.stringify({
                    RegionCoreModel: {
                        "Manager": $("#manager_name").val(),
                        "NoOfEmployee": $("#no_of_emplyee").val()
                    }
                });
                $.ajax({
                    url: '@Url.Action("UpdateRegion", "Store")',
                    type: "POST",
                    data: param,
                    contentType: 'application/json'
                }).success(function (response) {
                    console.log(response);
                }).error(function (response) {
                    console.log(response);
                });
            });

Controller

[HttpPost]
        public JsonResult UpdateRegion(RegionCoreModel region)
        {

            return Json(new{ manager= region.Manager, noofemployee = region.NoOfEmployee}, "text/html");
        }
War es hilfreich?

Lösung

This should work.

var param = JSON.stringify({
    region: {
        "Manager": $("#manager_name").val(),
        "NoOfEmployee": $("#no_of_emplyee").val()
    }
});

Andere Tipps

Try taking the properties out of quotes, like this:

var param = JSON.stringify({
    RegionCoreModel: {
        Manager: $("#manager_name").val(),
        NoOfEmployee: $("#no_of_emplyee").val()
    }

You don't the RegionCoreModel wrapper, and you probably don't need to convert to JSON yourself as well.

Try this:

var param = {
        "Manager": $("#manager_name").val(),
        "NoOfEmployee": $("#no_of_emplyee").val()
    };

Another thing to try is setting dataType explicitly, see last paran in below call:

$.ajax({
        url: '@Url.Action("UpdateRegion", "Store")',
        type: "POST",
        data: param,
        contentType: 'application/json',
        dataType: 'json'
    }).success(function (response) {
        console.log(response);
    }).error(function (response) {
        console.log(response);
    });
});
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top