简体   繁体   中英

Ajax send model to controller as JSON

I have a model I am trying to pass in from my view to my controller via an Ajax call which stringifies both the model and another string of data like this:

SetBinConfig: function (model, range) {
    var _model = JSON.stringify(model);
    var rangeSplit = range.split(/[ -]+/);
    var _rangeSplit = JSON.stringify(rangeSplit);

    var data = "model=" +_model + "&rangeSplit=" + _rangeSplit;

    $.ajax({
        url: '/IdentifiConfig/DefaultConfiguration/SetBinConfiguration',
        type: 'POST',
        data: "{'data' : '" + data + "'}",
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        cache: false,
        success: function (data) {
            if (data.error == 1) {
                cc.jqUtils.openDialog(data.ErrorDescription, 'Error', 'OK', null, null, null);
            }
        },
        error: function (x, h, r) {
            console.log(x, h, r);
        }
    });
},

Which is then received by this method:

public ActionResult SetBinConfiguration(string data)
{
        string range = data.Split(new string[] { "&rangeSplit=" }, StringSplitOptions.RemoveEmptyEntries)[1];
        string tempModelData = data.Split(new string[] {"&rangeSplit="}, StringSplitOptions.RemoveEmptyEntries)[0];
        string modelData = tempModelData.Replace("model=", "");

        DefaultConfigurationModel model = new JavaScriptSerializer().Deserialize<DefaultConfigurationModel>(modelData);

        string[] rangeSplit = Regex.Split(range, " - ");

        foreach (IdentifiBINConfiguration ibc in model.IdentifiBINConfigs)
        {
            if (ibc.LowerRange == rangeSplit[0] && ibc.UpperRange == rangeSplit[1])
            {
                model.IdentifiBINConfiguration = ibc;
                return Json(new { error = 0 });
            }
        }

        return Json(new { error = 1 });
}

However, I get this error:

The value "System.Collections.Generic.Dictionary`2[System.String,System.Object]" is not of type "IdentifiMessenger.Implementations.Identifi.Object.IdentifiBINConfiguration" and cannot be used in this generic collection. Parameter name: value

And I don't know what that means at all. I know what the Dictionary is, but why can I not just deserialize this object? I followed other answers right here on SO and I don't understand why this isn't working.

Edit: Model is quite literally my model, sent from my JS like this:

IdentifiConfig.SetBinConfig(@Html.Raw(Json.Encode(Model)), $('#BinRangesHidden').val());

And range is just a value from a hidden . I'm not posting back my model because I just need to modify one value and then have the page pull that modified value down later.

You still have to serialize your data.

data: JSON.stringify(data),

Then in your controller, your object should auto-parse:

public ActionResult SetBinConfiguration(MyData data)

Alternatively, you can parse it manually:

public ActionResult SetBinConfiguration(string data)
{
    MyData resumeDto = JsonConvert.DeserializeObject<MyData>(data);
...

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