发送JS数组= {}到C#(的WebMethod) [英] Send a JS Array = {} to C# (WebMethod)

查看:186
本文介绍了发送JS数组= {}到C#(的WebMethod)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实我已经声明了一个阵列上的JS一边象下面这样:

Actually I have a Array declared on JS side like below:

 var benefArray = {};
 var benefCount = 0;
 var benefNome = $('#txtBenefNome').val();
 var benefDataNasc = $('#txtBenefDataNasc').val();
 var benefGrauParent = $('#txtBenefGrauParent').val();

 benefCount++;
 benefArray[benefCount] = new Array(benefNome, benefDataNasc, benefGrauParent);

              //Ajax Sender
            function sendAjax(url, parametros, sucesso) {
                $.ajax({
                    type: "POST",
                    url: url,
                    data: parametros,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: sucesso
                });
            };

 sendAjax("Client.aspx/AddClient", "{benefArray: \"" + benefArray + "\"}",
 function (msg) {
                    var retorno = msg.d;
                    alert(retorno);
                });

在我的C#的WebMethod一边我有:

On my C# WebMethod side I Have:

    [WebMethod]
    public static string AddClient(object benefArray)
    {
        var t = benefArray;
    }

我试图让从Javascript的价值观,我有什么做的?
在这个任何见解将是AP preciated!谢谢

I'm trying to get those values from Javascript, what I have to do? Any insight on this will be appreciated! Thanks

推荐答案

通过定义一个模型,再present启动您正在使用,让你具有很强的工种,并摆脱了<$ C的工作数据在了addClient 方法$ C>对象丑:

Start by defining a model that will represent the data you are working with so that you work with strong types and get rid of the object ugliness on your AddClient method:

public class Benef
{
    public string Nome { get; set; }
    public string DataNasc { get; set; }
    public string GrauParent { get; set; }
}

然后让你的Web方法采取这种模式的数组:

then have your web method take an array of this model:

[WebMethod]
public static string AddClient(Benef[] benefs)
{
    // TODO: process ...

    // by the way as a result you could also return a strongly 
    // typed model and not only strings 
    // which could be easily manipulated on the client side
    return "some result"; 
}

和在客户端上您将定义的参数数组:

and on the client you would define an array of parameters:

var parameters = { 
    benefs: [
        {
            Nome: $('#txtBenefNome').val(),
            DataNasc: $('#txtBenefDataNasc').val(),
            GrauParent: $('#txtBenefGrauParent').val()
        }
    ]
};

$.ajax({
    type: 'POST',
    url: 'Client.aspx/AddClient',
    data: JSON.stringify(parameters),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function(result) {
        alert(result.d);
    }
});

据我使用这里的 JSON.stringify 方法来讲它是在现代浏览器本地。但是,如果你打算支持旧的浏览器,建议包括 json2.js 脚本到您的网页。

As far as the JSON.stringify method I am using here is concerned it is native in modern browsers. But if you intend to support older browsers it is recommended to include the json2.js script to your page.

这篇关于发送JS数组= {}到C#(的WebMethod)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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