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

查看:151
本文介绍了ASP.NET MVC如何从View传递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

            }

        ]
    };

我不知道该如何对象可以传递给控制器​​的方法,下面我有那里的时间表对象反映使用属性上面的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();
            }
        });

我,因为我没有使用的形式来操作数据看到了这个问题是相似,但并不完全相同。
<一href=\"http://stackoverflow.com/questions/267707/how-to-pass-complex-type-using-json-to-asp-net-mvc-controller\">http://stackoverflow.com/questions/267707/how-to-pass-complex-type-using-json-to-asp-net-mvc-controller

I have seen this question which is similar, but not quite the same as I am not using a forms to manipulate the data. http://stackoverflow.com/questions/267707/how-to-pass-complex-type-using-json-to-asp-net-mvc-controller

我也看到使用JsonFilter手动反序列化JSON引用,但不知道是否有一种方法可以做到这一点nativly虽然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的到来,因为它会被自动处理 - <一href=\"http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-$p$pview-1.aspx\">http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-$p$pview-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

您可以使用此ObjectFilter的:

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) { ... }

所以基本上,如果帖子的内容类型是应用/ 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如何从View传递JSON对象控制器参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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