ASP.NET MVC的ActionResult不读从Ajax请求JSON数据 [英] ASP.NET MVC ActionResult not reading json data from ajax request

查看:134
本文介绍了ASP.NET MVC的ActionResult不读从Ajax请求JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于在C#中读取一个JSON字符串的问题。我的情况是下面的。

I have a question about reading a JSON string in C#. My scenario is following.

我有ASP.NET MVC C#项目(.NET Framework 3.5的)。在LobbyController我有

I have ASP.NET MVC C# project(.NET framework 3.5). In LobbyController I have

. 
.
.
using System.IO;
using System.Web.Script.Serialization;
.
.
.
[HttpPost]
public ActionResult SomeMethod(string sampleData)
{
    //do stuff here
}

和jQuery的脚本文件我已经定义了点击功能

and in jquery script file I have defined click function

$("#buttonID").click(function() {
    var sampleData = {
            "property1": $('#elementID1').val(),
            "property2": $('#elementID2').val(),
            "property3": $('#elementID3').val()
        };

    $.ajax({
        url: "/Lobby.aspx/SomeMethod",
        type: "POST",
        data: sampleData,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function() { $('#message').html('Success').fadeIn(); },
        error: function() { $('#message').html('Error').fadeIn(); }
    });
});

现在,在和的someMethod单击ajax功能踢被称为控制器预期,但sampleData在参数为null。我也试过在这样的AJAX功能modifiing数据行:
数据:JSON.stringify(的sampleData),但它并没有正常工作。

Now, on click ajax function kicks in and SomeMethod is called in controller as expected but sampleData parameter is null. I also tried modifiing "data" line in ajax function like this: data: JSON.stringify(sampleData), but it didn't work as well.

我想提醒的sampleData对象的个别属性,他们有他们应该具有的价值,但由于某些原因ActionMethod的sampleData在参数为null。
有人能告诉我,我缺少的是什么?也许这是我的一个语法错误或有一些东西需要在C#方做了什么?

I tried to alert individual properties of sampleData object and they had value they're supposed to have but for some reason ActionMethod's sampleData parameter is null. Could someone tell me what I'm missing? Maybe it's a syntax error on my part or there is something that needs to be done on c# side?

任何帮助将AP preciated

Any help would be appreciated

推荐答案

感谢大家的帮助。

由于我用更多的答案组合,我将在这里总结一下。
我所要做的就是重新定义的sampleData对象jQuery中,其中属性名称未用引号括起来。然后我也跟着汤米的答案。最后,我改变了我的someMethod签名返回JsonResult类型和接受定制定义的模型类参数按whosrdaddy的回答。我想纪念你的帖子是有用的,但我没有足够的声誉呢。再次感谢大家的帖子。

Since I used combination of more answers I will sum it up here. What I had to do was to redefine sampledata object in jquery where property names were not enclosed in quotation marks. Then I followed Tommy's answer. Finally I changed my SomeMethod signature to return JsonResult type and accept custom defined model class as parameter as per whosrdaddy's answer. I'd mark your posts as useful but I don't have enough reputation yet. Once again thank you all for your posts.

只是一个快速的音符。在一点上没有。汤米的答案4,你需要引用Microsoft.Web.Mvc.dll组装你已经下载了,但我敢肯定,你会所有看着办吧。

Just a quick note. In point no. 4 of Tommy's answer you need to reference Microsoft.Web.Mvc.dll assembly you've downloaded but I'm sure you'd all figure it out.

这篇关于ASP.NET MVC的ActionResult不读从Ajax请求JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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