简体   繁体   English

将数据从Kendo网格发布到ASP.NET MVC控制器

[英]Issue posting data from a Kendo Grid to an ASP.NET MVC Controller

I'm having issues posting data to an MVC controller from a Kendo grid. 我在从Kendo网格向MVC控制器发布数据时遇到问题。 The specific action I'm trying to post looks something like this: 我要发布的特定操作如下所示:

public JsonResult Search(Credential searchParam)
{
    // Perform search
}

The Credential object it accepts is a POCO with an inner class and is defined as: 它接受的Credential对象是带有内部类的POCO,并且定义为:

public class Credential
{
    public class License
    {
        public string Prefix { get; set; }
        public string Number { get; set; }
        public string SubCategory { get; set; }

       // Constructors...
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public License LicenseNumber { get; set; }

    // Constructors...
}

I'm thinking my issue is with how the Kendo grid is sending the Credential my datasource is defined as: 我在想我的问题是Kendo网格如何发送我的数据源定义为的Credential

dataSource: {
    transport: {
        read: {
            url: Router.action("Search", "Index"),
            type: "post",
            contentType: "application/json",
            dataType: "json",
            data: {
                FirstName: $(".firstName").val(),
                LastName: $(".lastName").val(),
                LicenseNumber: {
                    Prefix: $(".cred1").val(),
                    Number: $(".cred2").val(),
                    SubCategory: $(".cred3").val()
                }
            }
        }
    },
    pageSize: 20
},

Currently, the controllers throws up with an error that says: Invalid JSON primitive: FirstName. 当前,控制器抛出错误,提示: Invalid JSON primitive: FirstName.

I've tried wrapping the field names in single and double quotes as well as tying to send the same data with JSON.stringify with no avail. 我尝试将字段名称包装在单引号和双引号中,并尝试使用JSON.stringify发送相同的数据而无济于事。

What am I doing wrong here? 我在这里做错了什么?

Did you try: 你试过了吗:

var license =  { 
                  LicenseNumber: {
                    'Prefix': $(".cred1").val(),
                    'Number': $(".cred2").val(),
                    'SubCategory': $(".cred3").val()
                  }
               }
var data = { 'FirstName': $(".firstName").val(), 'LastName': $(".lastName").val(), 'LicenseNumber': license }

dataSource: {
      // code
      data: JSON.stringify(data)
      // code
}

Try the following (note the additional quotation marks for data ): 请尝试以下操作(请注意data的其他引号):

dataSource: {
    transport: {
        read: {
            url: Router.action("Search", "Index"),
            type: "post",
            contentType: "application/json",
            dataType: "json",
            data: "{
                'FirstName': $(".firstName").val(),
                'LastName': $(".lastName").val(),
                'LicenseNumber': {
                    'Prefix': $(".cred1").val(),
                    'Number': $(".cred2").val(),
                    'SubCategory': $(".cred3").val()
                }
            }"
        }
    },
    pageSize: 20
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM