English 中文(简体)
序列化JSON对象在控制器上显示为Null
原标题:Serialized JSON Object appearing as Null at controller

我有一个字符串化的JSON对象,它在警报中正确显示,在浏览器调试器的post部分正确显示,,但在控制器中显示为null。我不确定它是怎么丢失信息的。

以下是JS代码:

 var assessmentStatus = [];

            $("select.assessmentSelect").each(function () {
                assessmentStatus.push({
                    assessmentname: $(this).attr("id"),
                    assessmentstatus: $(this).val()
                });


            });
alert(JSON.stringify(assessmentStatus));


            $.ajax({
                url:  @Url.Action("testAS") ,
                type: "POST",
                contentType:  application/json ,
                data: JSON.stringify({
                    AS: assessmentStatus,
                    AS2: assessmentStatus

                })

            });

这是警报中显示的内容:

[{"assessmentname":"testassessment","assessmentstatus":"Design"},{"assessmentname":"DepressionUpload","assessmentstatus":"Design"}]

这是帖子中出现的内容:

[Object { assessmentname="testassessment", assessmentstatus="Design"}, Object { assessmentname="DepressionUpload", assessmentstatus="Design"}]

我的控制器看起来像这样:

public ActionResult testAS (string[] AS, string AS2)

string[] AS returns [0]null  [1]null
string AS2 just returns null.

为什么它在发送到控制器时没有被字符串化?

最佳回答

您似乎试图传递一个具有2个属性的复杂对象数组:

assessmentStatus.push({
    assessmentname: $(this).attr("id"),
    assessmentstatus: $(this).val()
});

转换为一个简单的字符串数组:string[]为。这显然行不通。

因此,首先定义一个视图模型:

public class AssessmentViewModel
{
    public string Name { get; set; }
    public string Status { get; set; }
}

然后调整控制器动作:

public ActionResult TestAS(AssessmentViewModel[] assessments)
{
    ...
}

最后是你的javascript:

var assessments = [];
$( select.assessmentSelect ).each(function () {
    assessments.push({
        name: $(this).attr("id"),
        status: $(this).val()
    });
});
$.ajax({
    url:  @Url.Action("testAS") ,
    type: "POST",
    contentType:  application/json ,
    data: JSON.stringify({ assessments: assessments })
});
问题回答

对于看似简单的操作,您不必自己对参数进行编码。只需将一个带有属性的对象交给jQuery,它就会为你编码。

       $.ajax({
            url:  @Url.Action("testAS") ,
            type: "POST",
            contentType:  application/json ,
            data: {
                AS: assessmentStatus,
                AS2: assessmentStatus

            )

        });

我以前遇到过这种情况。您需要将jQuery的ajaxSettings.tradial设置为true。不久前,他们改变了序列化数组的方式。它把asp.net mvc搞砸了。

这个问题谈到了它:

如何在没有表单的情况下将字符串数组发布到ASP.NET MVC控制器

as does this: http://forum.jquery.com/topic/nested-param-serialization

因此,只需输入:

jQuery.ajaxSettings.traditional = true;

然后像Pointy说的那样做,不要把数据串起来。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签