使用表单序列化的JSON对象帖子不映射到c#对象

时间:2013-04-02 05:33:45

标签: c# jquery asp.net-mvc json

我正在使用带有JSON对象的ajax帖子作为强类型视图中的数据。表单序列化工作正常但在获取模型的控制器中,属性未被映射。在Firebug中,我正在获取以下快照Firebug post states

如何将序列化的JSON对象映射到c#object

3 个答案:

答案 0 :(得分:2)

您必须将jquery扩展名复制到convert a form to json object

(function() {
    $.fn.serializeObject = function() {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
                    if (o[this.name]) {
                        if (!o[this.name].push) {
                            o[this.name] = [o[this.name]];
                        }
                        o[this.name].push(this.value || '');
                    } else {
                        o[this.name] = this.value || '';
                    }
                });
        return o;
    };
})(jQuery)

并添加json2.js库以添加对旧版浏览器的JSON.stringify()支持

然后将ajax改为

$.ajax({
    url : '',
     type: 'POST',
    contentType : 'application/json',
    data : JSON.stringify($('form').serializeObject()),
     ....
})

在你的情况下

$('#frm').submit(function() {

    postJSON('@Url.Action("Create", "District")', JSON.stringify($('form').serializeObject()),
            function(result) {
                alert(result.Message);
                if (result.Success) {
                    window.location = '@Url.Action("Index", "District")';
                }
            });
    return false;
});

答案 1 :(得分:1)

这只是一个标准的序列化表格。它不是JSON。

您可能需要查看this

答案 2 :(得分:0)

如果你还要包含复选框和单选按钮,可以使用@Arun_P_Johny回答,只需要用serializeArray覆盖:

$.fn.serializeArray = function (options) {
var o = $.extend({
    checkboxesAsBools: false
}, options || {});

var rselectTextarea = /select|textarea/i;
var rinput = /text|hidden|password|search/i;

return this.map(function () {
    return this.elements ? $.makeArray(this.elements) : this;
})
.filter(function () {
    return this.name && !this.disabled &&
        (this.checked
        || (o.checkboxesAsBools && this.type === 'checkbox')
        || rselectTextarea.test(this.nodeName)
        || rinput.test(this.type));
})
    .map(function (i, elem) {
        var val = $(this).val();
        return val == null ?
        null :
        $.isArray(val) ?
        $.map(val, function (val, i) {
            return { name: elem.name, value: val };
        }) :
        {
            name: elem.name,
            value: (o.checkboxesAsBools && this.type === 'checkbox') ? (this.checked ? 'true' : 'false') : val
        };
    }).get();
};

serializeObject应该被修改

$.fn.serializeObject = function () {
     var o = {};
     var a = this.serializeArray({ checkboxesAsBools: true });
     $.each(a, function () {
             if (o[this.name] !== undefined) {
                     if (!o[this.name].push) {
                             o[this.name] = [o[this.name]];
                         }
                     o[this.name].push(this.value || '');
                 } else {
                     o[this.name] = this.value || '';
                 }
         });
     return o;
};