简体   繁体   English

Ajax将模型作为JSON发送到控制器

[英]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: 我有一个模型,我正在尝试通过Ajax调用将其从视图传递到控制器,该调用对模型和另一个数据字符串都进行了字符串化,如下所示:

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. 我在SO上遵循了其他答案,但我不明白为什么这不起作用。

Edit: Model is quite literally my model, sent from my JS like this: 编辑: Model从字面上是我的模型,从我的JS发送像这样:

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

And range is just a value from a hidden . range只是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);
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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