using the result from jQuery into C#

拜拜、爱过 提交于 2020-01-15 11:43:08

问题


I have this function in jquery which has the result array and how can I get this result array to C# code. Can anyone help me regarding this.

 function generateData() {
 var result = $('#accordion').sortable('toArray');
 }

回答1:


You could do this asynchronously through a web method call from script, such that you define a web method appropriately, then call and handle the data and potential return value, as desired. For example:

Defining a web method:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string HandleData(object[] data)
{
    //handle data
    return string.Empty;
}

Defining a reusable jQuery script method to handle web method calls:

function ExecutePageMethod(page, fn, paramArray, successFn, errorFn) {
    var paramList = '';
    if (paramArray.length > 0) {
        for (var i = 0; i < paramArray.length; i += 2) {
            if (paramList.length > 0) paramList += ',';
            paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
        }
    }
    paramList = '{' + paramList + '}';
    $.ajax({
        type: "POST",
        url: page + "/" + fn,
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "json",
        success: successFn,
        error: errorFn
    });
}

And, of course, the call itself:

ExecutePageMethod("Default.aspx", "HandleData", 
    ["data", result], successCallback, failureCallback);

Naturally we now need to make sure our callback methods exist:

function successCallback(result) {
    var parsedResult = jQuery.parseJSON(result.d);
}

function failureCallback(result) {

}



回答2:


Use a hiddenfield to store the result..

<asp:HiddenField id="hfResult" runat="server" />

JQuery

$('hfResult').val(result);

C#

String result = hfResult.Value;

Note that a hiddenField only holds a string, so you might need to use some kind of seperator to seperate your array objects..



来源:https://stackoverflow.com/questions/7740545/using-the-result-from-jquery-into-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!