简体   繁体   English

将JSON objet传递到代码隐藏文件

[英]Passing JSON objet to Code Behind file

I have used ajax to pass the JSON object from javascript function to code behind file (.cs) in C# with the following code as follows: 我已经使用ajax通过以下代码将javascript对象中的JSON对象从javascript函数传递到文件(.cs)中的代码,如下所示:

 function buildProfile(user) {

            alert('user data');
            $.ajax({
                    type: 'POST',
                    url: '/Test.aspx/GetCity',
                    data: "{city:" + JSON.stringify(user) + "}",
                    //data: JSON.stringify(user),
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success: function (r) {
                        alert(r.d.Name);
                    }
                });
}

In the above code the control is going successfully to the Test.aspx.cs codebehind file to the method name " GetCity ". 在上面的代码中,控件已成功转到方法名称为“ GetCity ”的Test.aspx.cs代码隐藏文件。

Can I pass the JSON object to a user control rather than to a page? 我可以将JSON对象传递给用户控件而不是页面吗? For example can I create a user control with name Test and change the url of the above code to : url: '/Test.ascx/GetCity', the code will become as follows: 例如,我可以创建一个名为Test的用户控件,并将上述代码的url更改为:url:'/Test.ascx/GetCity',代码将如下所示:

 function buildProfile(user) {

            alert('user data');
            $.ajax({
                    type: 'POST',
                    url: '/Test.ascx/GetCity',
                    data: "{city:" + JSON.stringify(user) + "}",
                    //data: JSON.stringify(user),
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success: function (r) {
                        alert(r.d.Name);
                    }
                });
}

For now the control is not going to the user control Test.aspx.cs code behind file. 目前,该控件不会转到文件后面的用户控件Test.aspx.cs代码。 Is it possible to pass the JSON object to a user control? 是否可以将JSON对象传递给用户控件?

Any help will be greatly appreciated. 任何帮助将不胜感激。 Thanks in advance. 提前致谢。

Not sure If I got your need right but I had a similar need a couple months ago and maybe this link can help (please see Ajith S Nair comment) : https://forums.asp.net/t/2022191.aspx?How+to+create+a+user+control+in+MVC . 不确定我是否满足您的需求,但几个月前我有类似的需求,也许此链接可以提供帮助(请参阅Ajith S Nair的评论): https : //forums.asp.net/t/2022191.aspx?如何+在+ MVC中创建+ a + user + control +

If you combine his extension with something similar to your ajax code it might be your solution : 如果将他的扩展名与类似于ajax代码的东西结合使用,则可能是您的解决方案:

!function ($) {
"use strict";
$.ajax({
    type: "POST",
    url: "/ControllerName/MethodName",
    data: "{city:" + JSON.stringify(user) + "}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {//Success code},
    failure: function (response) {//Failure code},
    error: function (response) {//Error code}
})};

I hope it helps ! 希望对您有所帮助!

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

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