将Json数据传递给MVC控制器——操作方法的JObject参数

时间:2021-05-15 21:06:30

Sorry for the wrong formatting or spelling. As i am writting this post in hurry. :)

对不起,格式或拼写错误。因为我正在匆忙地写这篇文章。:)

I am passing json data to action method of simple controller (MVC controller). Here is the below sample code for reference.

我正在将json数据传递给simple controller (MVC controller)的action方法。下面是示例代码以供参考。

JS CODE:

JS代码:

var json = {IsInit :true, SearchParam:{ Type:"xx",Name:"xx",sort: ""} };

var json = {IsInit:true, SearchParam:{Type:"xx",Name:"xx",sort: "};

    Nx$(document).ready(function () {

        Nx$.ajax({
            async: true,
            contentType: "application/json; charset=utf-8",
            type: "POST",
            url: "Home/Data",
            dataType: "json",
            data: JSON.stringify(json),
            success: function (msg) {
                alert('success');
            },
            error: function (jqxhr, textStatus, error) {
                alert('fail');
            }
        });

Action Method :

操作方法:

<HttpPost>
        <Route("Data")>
        Function GetData(ByVal IsInit As Boolean, ByVal SearchParam As Newtonsoft.Json.Linq.JObject) As ActionResult
            Return Nothing
        End Function

Now, above action doesn't call at all. but if you use below code then it makes the call but SearchParam is of type [object] only and you can not use any value of searchParam object.

现在,以上的行动根本不需要。但是如果您使用下面的代码,那么它将发出调用,但是SearchParam是类型为[object]的,并且您不能使用SearchParam对象的任何值。

<HttpPost>
    <Route("Data")>
    Public Function GetData(ByVal IsInit As Boolean, ByVal SearchParam As Object) As Object
        Return Nothing
    End Function

I think that it does not able to pass multiple parameter in POST request with complex json object.

我认为它不能用复杂的json对象在POST请求中传递多个参数。

How can i get JSON data passed to MVC controller's action method so that SearchParam Json data converted to JObject it self. i.e. initial first Action method signature should be used with out any major method signature changes.

如何将JSON数据传递给MVC控制器的操作方法,以便将SearchParam JSON数据转换为JObject it self。例如,初始优先操作方法签名应该与任何主要方法签名更改一起使用。

Few Observation :

一些观察:

  1. if i turned above action method as api in API Controller; it start working but having said that you need to follow the below method signature. below approach is not working for MVC controller. Don't know why ?? However, For Some reason; i can not go with API controller.

    如果我在api控制器中将以上操作方法作为api;它开始工作,但是您需要遵循下面的方法签名。下面的方法不适用于MVC控制器。不知道为什么? ?然而,由于某些原因;我不能使用API控制器。

        <HttpPost>
        <Route("Data")>
        Function GetData(ByVal req As Newtonsoft.Json.Linq.JObject) As ActionResult
            Return Nothing
        End Function
    

Thanks !!

谢谢! !

2 个解决方案

#1


2  

Better to never use JOBject for this. Instead it is much more practical and highly recommended to create and use a Model class. MVC Web API will then bind the incoming JSON to a Model object, all for free.

最好不要使用JOBject。相反,创建和使用模型类更实用,并强烈推荐使用模型类。MVC Web API会将传入的JSON绑定到一个模型对象,所有这些都是免费的。

Example:

例子:

Public Class MyData
    Public Property IsInit As Boolean
    Public Property Type As String
    Public Property Name As String
    Public Property Sort As String
End Class

Note that here I piled everything into one Class. If you want to keep the IsInit separate from the rest then you can split it like this:

注意,这里我把所有东西都放到一个类中。如果你想把IsInit与其他的分离开来,那么你可以这样分割它:

Public Class Param
    Public Property Type As String
    Public Property Name As String
    Public Property Sort As String
End Class

Public Class MyData
    Public Property IsInit As Boolean
    Public Property SearchParam as Param
End Class

Then change your Action Method like this:

然后改变你的行动方法如下:

Function GetData(<FromBody()> ByVal data As MyData) As ActionResult
    ...
End Function

Finally you probably need to use data: json in your call, so without calling JSON.stringify().

最后,您可能需要使用数据:在您的调用中使用json,因此无需调用JSON.stringify()。

#2


0  

Thanks Peter!! Your comment gave me direction. Below code snippet used to wrap JSON data to Dictionary Object. Here i understand your point for not using JObject and instead rely on Model object. But for some reason; i have to use dictionary object to get JSON due to dynamic JSON nature from other end point.

谢谢彼得! !你的评论给我指明了方向。下面的代码片段用于将JSON数据包装到Dictionary对象。在这里,我理解您不使用JObject而依赖模型对象的观点。但是由于某些原因;由于其他端点的动态JSON特性,我不得不使用dictionary对象来获取JSON。

Public Class MyData
            Public Property IsInit As Boolean
            Public Property SearchParam As Dictionary(Of Object, Object)
        End Class

Thanks a lot !!

非常感谢! !

#1


2  

Better to never use JOBject for this. Instead it is much more practical and highly recommended to create and use a Model class. MVC Web API will then bind the incoming JSON to a Model object, all for free.

最好不要使用JOBject。相反,创建和使用模型类更实用,并强烈推荐使用模型类。MVC Web API会将传入的JSON绑定到一个模型对象,所有这些都是免费的。

Example:

例子:

Public Class MyData
    Public Property IsInit As Boolean
    Public Property Type As String
    Public Property Name As String
    Public Property Sort As String
End Class

Note that here I piled everything into one Class. If you want to keep the IsInit separate from the rest then you can split it like this:

注意,这里我把所有东西都放到一个类中。如果你想把IsInit与其他的分离开来,那么你可以这样分割它:

Public Class Param
    Public Property Type As String
    Public Property Name As String
    Public Property Sort As String
End Class

Public Class MyData
    Public Property IsInit As Boolean
    Public Property SearchParam as Param
End Class

Then change your Action Method like this:

然后改变你的行动方法如下:

Function GetData(<FromBody()> ByVal data As MyData) As ActionResult
    ...
End Function

Finally you probably need to use data: json in your call, so without calling JSON.stringify().

最后,您可能需要使用数据:在您的调用中使用json,因此无需调用JSON.stringify()。

#2


0  

Thanks Peter!! Your comment gave me direction. Below code snippet used to wrap JSON data to Dictionary Object. Here i understand your point for not using JObject and instead rely on Model object. But for some reason; i have to use dictionary object to get JSON due to dynamic JSON nature from other end point.

谢谢彼得! !你的评论给我指明了方向。下面的代码片段用于将JSON数据包装到Dictionary对象。在这里,我理解您不使用JObject而依赖模型对象的观点。但是由于某些原因;由于其他端点的动态JSON特性,我不得不使用dictionary对象来获取JSON。

Public Class MyData
            Public Property IsInit As Boolean
            Public Property SearchParam As Dictionary(Of Object, Object)
        End Class

Thanks a lot !!

非常感谢! !