通过ajax将jquery中的数组传递给c#webmethod [英] pass an array in jquery via ajax to a c# webmethod

查看:147
本文介绍了通过ajax将jquery中的数组传递给c#webmethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数组传递给C#网络方法,但没有一个很好的例子可循.感谢您的协助.

I'd like to pass an array to a c# webmethod but don't have a good example to follow. Thanks for any assistance.

这是我到目前为止所拥有的:

Here is what I have so far:

我的数组:

$(".jobRole").each(function (index) {

    var jobRoleIndex = index;
    var jobRoleID = $(this).attr('id');
    var jobRoleName = $(this).text();

    var roleInfo = {
        "roleIndex": jobRoleIndex,
        "roleID": jobRoleID,
        "roleName": jobRoleName
    };

    queryStr = { "roleInfo": roleInfo };
    jobRoleArray.push(queryStr);

});

我的Ajax代码

  $.ajax({
            type: "POST",
            url: "WebPage.aspx/save_Role",
            data: jobRoleArray,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            success: function (data) {
                alert("successfully posted data");
            },
            error: function (data) {
                alert("failed posted data");
                alert(postData);
            }

        });

不确定网络方法,但这是我在想什么:

Not sure on the webmethod but here is what I'm thinking:

[WebMethod]
    public static bool save_Role(String jobRoleArray[])

推荐答案

您将传递以下数组:

[
    "roleInfo": { 
              "roleIndex": jobRoleIndex, 
               "roleID": jobRoleID, 
               "roleName": jobRoleName 
     },
     "roleInfo": { 
              "roleIndex": jobRoleIndex, 
               "roleID": jobRoleID, 
               "roleName": jobRoleName 
     }, ...
]

在我看来,如果您有一个与该结构相匹配的类,它将变得更加容易,例如:

And in my opinion, it would be easier if you have a class that matches that structure, like this:

public class roleInfo
{
     public int roleIndex{get;set;}    
     public int roleID{get;set;}
     public string roleName{get;set;}
}

这样,当您从jQuery调用Web方法时,您可以这样做:

So that when you call your web method from jQuery, you can do it like this:

 $.ajax({
            type: "POST",
            url: "WebPage.aspx/save_Role",
            data: "{'jobRoleArray':"+JSON.stringify(jobRoleArray)+"}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            success: function (data) {
                alert("successfully posted data");
            },
            error: function (data) {
                alert("failed posted data");
                alert(postData);
            }

        });

在您的网络方法中,您可以通过以下方式接收List<roleInfo>:

And in your web method, you can receive List<roleInfo> in this way:

[WebMethod]
public static bool save_Role(List<roleInfo> jobRoleArray)
{

}

如果您尝试此操作,请告诉我.上面的代码没有经过任何测试,因此可能会出现错误,但是我认为这是一种很好且非常干净的方法.

If you try this, please let me know. Above code was not tested in any way so there might be errors but I think this is a good and very clean approach.

这篇关于通过ajax将jquery中的数组传递给c#webmethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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