ASP.NET MVC可以JSON对象传递到与动态类型参数参数的控制器? [英] ASP.NET MVC Can JSON object be passed to a controller with parameter with dynamic type parameter?

查看:1309
本文介绍了ASP.NET MVC可以JSON对象传递到与动态类型参数参数的控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code。在我的javascript,一个AJAX发布数据:

I have code in my javascript, a ajax to post data:

$.ajax({
                url: '/Configurations/GetSelectedPageTranslation',
                type: 'POST',
                data: { inttype: $("#some").val(), objectType:{prop1: 'adsfa', prop2: 'asdf'}},
                success: function (result) {

                },
                error: function () {
                    alert('error');
                }
            });

在位指示我有签名的方法:

In the contoller i have a method with signature:

public JsonResult GetSelectedPageTranslation(int inttype, dynamic objectType)

我可以正确有IntType上。但是对象类型将不能为空,但如果我不喜欢objectType.prop1,它会抛出错误。如果我将JSON.stringify在JavaScript中的对象类型,在控制器对象类型将有一个字符串值。

I can have the inttype correctly. However the objectType will not be null but if i do like objectType.prop1, it will throw error. If i will JSON.stringify the object type in the javascript, the objectType in the controller will have a string value.

难道这是可能使用这样的动态数据类型直接访问JSON数据控制器:objectType.prop1

Could this be possible to directly access the JSON data in the controller using the dynamic data type like this: objectType.prop1 ?

感谢

推荐答案

没有开箱即支持在MVC3 动态操作参数。

There is no out of the box support for dynamic action arguments in MVC3.

不过MVC是非常可扩展的,因此您可以添加此功能。你需要做的是创建一个自定义 IModelBinder 在这里你可以做的JSON反序列化,并建立一个动态的对象。

However MVC is very extensible so you can add this functionality. What you need to do is to create a custom IModelBinder where you can do the JSON deserialization and build up a dynamic object.

幸运的是,这篇文章:制作MVC 3多一点...动态是处理完全相同的问题,所以你也可以找到样本code和其他链接也应该帮助你开始。

Luckily this article: Making MVC 3 a little more… dynamic is dealing with the exact same problem, so you can find also sample code and additional links there which should help you get started.

这篇关于ASP.NET MVC可以JSON对象传递到与动态类型参数参数的控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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