将JSON对象和对象列表传递给ASP.Net控制器 [英] Passing JSON Object and List of Objects to ASP.Net Controller

查看:84
本文介绍了将JSON对象和对象列表传递给ASP.Net控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在下面提供一些帮助.我有这两个模型,下面将使用它们的方法.

I need some help with the hereunder please. I have these 2 models and the method I will be using them in hereunder.

public class RoleModel
{
    public string Name { get; set; }
    public string Description { get; set; }
    public List<PermissionGroupModel> PermissionGroups { get; set; }
}

public class PermissionGroupModel
{
    public int PermissionGroupID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

 [HttpPost]
 public bool CreateRole(RoleModel r){
  //code goes here
}

我试图通过AJAX将数据发布到CreateRole()方法,并使用调试中断来检查'r'参数,以查看是否填充了模型.这是我用来测试的AJAX调用.

I am trying to post data to the CreateRole() method via AJAX and using a debug break to inspect the 'r' parameter to see if the model is getting populated. This is the AJAX call I am using to test it out.

$.ajax({
        type: "POST",
        url: "/BackOffice/CreateRole",
        data:  {"Name": "Test Name", "Description": "Test Desc", "PermissionGroups": ["Name": "Group Name", "Description": "Test Desc"]},
        success: function (data) {},
        complete: function (data) {}
      });

发出请求后,我在Visual Studio中检查了参数.将填充RoleModel名称和描述键,并且PermissionGroups计数为1,但不会填充PermissionGroups中的键.

When the request is made and I inspect the parameter in visual studio. The RoleModel Name and Description Keys are populated and PermissionGroups Count is 1 but the keys in PermissionGroups are not being populated.

任何人都可以建议我通过其中包含对象列表的JSON对象的任何方式.

Can anyone suggest any way I can pass a JSON object with a list of objects in it.

谢谢.

推荐答案

您发送的javascript对象不正确.

The javascript object you are sending is not proper.

 "PermissionGroups": ["Name": "Group Name", "Description": "Test Desc"]

在您的视图模型中,PermissionGroupsPermissionGroupModel个项目的集合.但是您不会像这样通过它.上面的代码发送4个字符串项目作为PermissionGroups属性的值.您需要使用{}

In your viewmodel, PermissionGroups is a collection of PermissionGroupModel items. But you are not passing it like this. The above code is sending 4 string items as the value of the PermissionGroups property. You need to wrap each item in the collection with { and }

此外,要使模型绑定发生在复杂对象上,您需要使用JSON.stringify方法将javascript对象转换为json字符串,并在进行ajax调用时指定contentType属性. contentType属性值应为"application/json"

Also ,for model binding to happen on complex objects, you need to convert your javascript object to a json string using JSON.stringify method and specify the contentType property when making the ajax call. The contentType property value should be "application/json"

contentType属性告诉服务器我们正在发送的数据为Json格式.

The contentType property tells the server that the data we are sending is in Json format.

 var d = {
            "Name": "Test Name", "Description": "Test Desc",
            "PermissionGroups": [{ "Name": "Group Name", "Description": "Test Desc" }]
         };

 $.ajax({
            type: "POST",
            url: "/BackOffice/CreateRole",
            contentType: "application/json",
            data: JSON.stringify(d) ,
            success: function (data) {  console.log(data); },
            complete: function (data) {}
       });

此外,最好使用Url.Action帮助程序方法来构建操作方法的网址.

Also, It is a good idea to use the Url.Action helper method to build the urls to action methods.

因此将url: "/BackOffice/CreateRole"替换为url: "@Url.Action("CreateRole","BackOffice")"

这篇关于将JSON对象和对象列表传递给ASP.Net控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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