JSON对象post使用表单序列化未映射到c#对象

JSON对象post使用表单序列化未映射到c#对象,c#,jquery,asp.net-mvc,json,C#,Jquery,Asp.net Mvc,Json,我使用带有JSON对象的ajax post作为强类型视图中的数据。表单序列化工作正常,但在控制器中,在获取模型时,不会映射属性。在Firebug中,我获得以下快照 如何将序列化的JSON对象映射到c#对象,这只是一种标准的序列化形式。它不是JSON 您可能需要查看。您必须将jquery扩展复制到 并包含库以添加对旧式浏览器的JSON.stringify()支持 然后将您的ajax更改为 $.ajax({ url : '', type: 'POST', contentTy

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


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

,这只是一种标准的序列化形式。它不是JSON


您可能需要查看。

您必须将jquery扩展复制到

并包含库以添加对旧式浏览器的
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;
});

如果您还想包括复选框和单选按钮,可以使用@Arun_p_Johny answer,数组也应该被以下内容覆盖:

$.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();
};
应修改序列化对象

$.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;
};

您没有在这里提交json对象,json值将类似于
{DistrictID:'13',RegionID:'1',…}
您可以共享javascript吗?那么我该如何做?“$('#frm')。提交(函数(){postJSON('@Url.Action(“Create”,“District”),$('#frm')。序列化(),函数(结果){alert(result.Message);if(result.Success){window.location='@Url.Action(“Index”,“District”);}};返回false;})`用于使用post,而postJSON是我的自定义json对象post jquery函数我如何将表单序列化对象映射到json对象。。??
$.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;
};