ASP.NET MVC 如何将 JSON 对象作为参数从视图传递到控制器 [英] ASP.NET MVC How to pass JSON object from View to Controller as Parameter

查看:30
本文介绍了ASP.NET MVC 如何将 JSON 对象作为参数从视图传递到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的 JSON 对象,它可以毫无问题地发送到视图(如下所示),但是当它通过 AJAX 调用传递回控制器时,我无法弄清楚如何将此数据序列化回 .NET 对象.各个部分的详细信息如下.

I have a complex JSON object which is sent to the View without any issues (as shown below) but I cannot work out how Serialize this data back to a .NET object when it is passed back to the controller through an AJAX call. Details of the various parts are below.

   var ObjectA = {
        "Name": 1,
        "Starting": new Date(1221644506800),

        "Timeline": [
            {
                "StartTime": new Date(1221644506800),
                "GoesFor": 200

            }
            ,
            {
                "StartTime": new Date(1221644506800),
                "GoesFor": 100

            }

        ]
    };

我不确定如何将这个对象传递给控制器​​方法,我在下面有这个方法,其中 Timelines 对象使用属性来镜像上面的 JS 对象.

I am not sure how this object can be passed to a Controller Method, I have this method below where the Timelines object mirrors the above JS object using Properties.

public JsonResult Save(Timelines person)

我使用的 jQuery 是:

The jQuery I am using is:

        var encoded = $.toJSON(SessionSchedule);

        $.ajax({
            url: "/Timeline/Save",
            type: "POST",
            dataType: 'json',
            data: encoded,
            contentType: "application/json; charset=utf-8",
            beforeSend: function() { $("#saveStatus").html("Saving").show(); },
            success: function(result) {
                alert(result.Result);
                $("#saveStatus").html(result.Result).show();
            }
        });

我已经看到了这个类似的问题,但并不完全相同,因为我没有使用表单来操作数据.如何使用json传递复杂类型到ASP.NET MVC 控制器

I have seen this question which is similar, but not quite the same as I am not using a forms to manipulate the data. How to pass complex type using json to ASP.NET MVC controller

我还看到了使用JsonFilter"手动反序列化 JSON 的参考,但想知道是否有一种方法可以通过 ASP.NET MVC 原生地做到这一点?或者以这种方式传递数据的最佳做法是什么?

I have also seen references to using a 'JsonFilter' to manually deserialize the JSON, but was wondering if there is a way to do it nativly though ASP.NET MVC? Or what are the best practices for passing data in this way?

推荐答案

MVC 3 的到来不再需要这种方法,因为它将被自动处理 - http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-preview-1.aspx

This method should no longer be needed with the arrival of MVC 3, as it will be handled automatically - http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-preview-1.aspx

你可以使用这个对象过滤器:

You can use this ObjectFilter:

    public class ObjectFilter : ActionFilterAttribute {

    public string Param { get; set; }
    public Type RootType { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext) {
        if ((filterContext.HttpContext.Request.ContentType ?? string.Empty).Contains("application/json")) {
            object o =
            new DataContractJsonSerializer(RootType).ReadObject(filterContext.HttpContext.Request.InputStream);
            filterContext.ActionParameters[Param] = o;
        }

    }
}

然后您可以将其应用到您的控制器方法中,如下所示:

You can then apply it to your controller methods like so:

    [ObjectFilter(Param = "postdata", RootType = typeof(ObjectToSerializeTo))]
    public JsonResult ControllerMethod(ObjectToSerializeTo postdata) { ... }

所以基本上,如果帖子的内容类型是application/json",这将生效并将值映射到您指定的类型的对象.

So basically, if the content type of the post is "application/json" this will spring into action and will map the values to the object of type you specify.

这篇关于ASP.NET MVC 如何将 JSON 对象作为参数从视图传递到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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