jQuery Ajax并将多个复杂对象发布到asp.net MVC Controller [英] JQuery Ajax and Posting multiple complex objects to asp.net MVC Controller

查看:164
本文介绍了jQuery Ajax并将多个复杂对象发布到asp.net MVC Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

`问候,

在将多个参数发布到mc控制器方法时遇到问题.

Having problem posting multiple parameters to an mc controller method.

controller...
[HttpPost]
public ActionResult SaveSomething(SomeDomainObject domainObject, bool anOption)
{
}

Ajax Code...
function performPostData(postDataOptions,closeWindow) {
        $.ajax({ type: postDataOptions.httpVerb,
            url: postDataOptions.url,
            datatype: "json",
            traditional: true,
            data: postDataOptions.data(),
            success: function () {
                if (closeWindow) {
                    var window = $('#window').data("kendoWindow");
                    window.close();             
                }
            },
            error: function (xhr, status, error) {
                var msg = JSON.parse(xhr.responseText);
                var validator = $("#editBase").kendoValidator().data("kendoValidator"),
                status = $(".editValidationStatus");
                var err = msg.Message;
                status.text(err).addClass("invalid");
            }
        });
    }

JScript function to format data...

function GetPostData()
{
    var _domainObject={            
            prop1: 1,
            prop2: 2,
            prop3: 3                      
        };
        return JSON.stringify({ domainObject:_domainObject,anOption: true});
}

我在网络捕获中看到这是请求正文:

I seeing in network capture that this is the request body:

{"domainObject":{"prop1":1,"prop2":2,"Prop3":3},"anOption":true}

控制器引发以下异常:

The parameters dictionary contains a null entry for parameter 'anOption' of non-nullable type 'System.Boolean' for method 'System.Web.Mvc.ActionResult SaveSomething(Domain.Data.SomeDomainObject, Boolean)' in 'Reports.Controllers.TestController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

我花了好几个小时试图获取一个简单的域对象和另一个布尔类型的参数进行序列化并将其传递给我的控制器.有什么想法吗?

I have been spinning my wheels for hours trying to get a simple domain object and another parameter of type bool to be serialized and passed to my controller. Any ideas?

推荐答案

好像您在jquery ajax调用中缺少内容类型参数. 您应该将其显式设置为json,如以下代码所示:

Looks like you are missing the content type parameter in the jquery ajax call. You should explicitly set it as json, as in this piece of code:

function performPostData(postDataOptions,closeWindow) {
    $.ajax({ type: postDataOptions.httpVerb,
        url: postDataOptions.url,
        datatype: "json",
        contentType: "application/json; charset=utf-8",

        ...rest of your code

否则,它将获得其默认值(请参见 http://api.jquery.com/jQuery.ajax/):

Otherwise it will get its default value (see http://api.jquery.com/jQuery.ajax/):

application/x-www-form-urlencoded; charset = UTF-8

application/x-www-form-urlencoded; charset=UTF-8

这篇关于jQuery Ajax并将多个复杂对象发布到asp.net MVC Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆