在服务器端获取JSON格式的数据

时间:2014-04-29 05:46:29

标签: javascript jquery asp.net-mvc asp.net-mvc-3

我使用jquery AJAX作为::

传递JSON Stringified数据
 $("#btnTest").click(function () {
        var models = [];
        models.push({ TaskID: 4, OwnerID: 1, RecurrenceID: null, Title: "Test Title213", Description: "Desc... "});
        $.ajax({
                type: 'POST',
                url:"/Home/Test_UpdateSchedule/",
                data: {models:JSON.stringify(models)},
                traditional: true,
                success: function (data)
                {
                    //
                }
        });
    });

在服务器端,我试图以::

的形式访问它
public ActionResult Test_UpdateSchedule(List<SchedulerViewModel> models)
{
         //Code Implementation
}

但我没有在服务器端获取数据。我将Count计为0。

我的ViewModel是::

public class SchedulerViewModel 
    {
        public int TaskID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }

        public string RecurrenceRule { get; set; }
        public int? RecurrenceID { get; set; }
        public string RecurrenceException { get; set; }
        public bool IsAllDay { get; set; }
        public int? OwnerID { get; set; }
        public string StartTimezone { get; set; }
        public string EndTimezone { get; set; }

    }

如何在服务器端访问此处的数据?

2 个答案:

答案 0 :(得分:0)

只需更改您的JSON.stringify(),如下所示。

    JSON.stringify({ models: models })

现在正在处理ajax帖子。

    $.ajax({
        type: 'POST',
        url: '@Url.Action("Test_UpdateSchedule", "Home")',
        data: JSON.stringify({ models: models }),
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            //
        }
    });

答案 1 :(得分:-2)

使用Org.json lib将json字符串转换为json对象

JSONObject jsonObj = new JSONObject(“your parameter string”);

在覆盖到json对象后,您可以在您的课程中进行迭代和设置。