如何使用jQuery的AJAX请求来调用ChangeLoginStatus函数?

huangapple go评论74阅读模式
英文:

How to make jquery ajax request to call function ChangeLoginStatus?

问题

我在asp.net razor页面上工作。我需要发出ajax请求来调用函数ChangeLoginStatus,并在LoginStatusMsg上返回值。

[BindProperty]
public string LoginStatusMsg { get; set; }

[HttpGet]
public JsonResult OnGetChangeLoginStatus(string UserId)
{
    string strHost = _ISecurityService.GetPCName();
    DataSet ds = _IUserSecurity.ChangeLoginStatus(UserId, strNewStatus, UserId, strHost);
    if (ds.Tables[0].Rows.Count > 0)
    {
        if (ds.Tables[0].Rows[0][0].ToString() != "0")
        {
            LoginStatusMsg = ds.Tables[0].Rows[0][1].ToString();
        }
    }

    return new JsonResult(LoginStatusMsg);
}

在razor cshtml中:

<span class="spn col-lg-2">账户状态:@Model.LoginStatusMsg</span>
英文:

I work on asp.net razor page . I need to make ajax request to call function

ChangeLoginStatus and return value on LoginStatusMsg

    [BindProperty]
    public string LoginStatusMsg { get; set; }

   [HttpGet]

        public JsonResult OnGetChangeLoginStatus(string UserId)
        {
          
            string strHost = _ISecurityService.GetPCName();
            DataSet ds = _IUserSecurity.ChangeLoginStatus(UserId, strNewStatus, UserId, strHost);
            if (ds.Tables[0].Rows.Count &gt; 0)
            {
                if (ds.Tables[0].Rows[0][0].ToString() != &quot;0&quot;)
                {
                    LoginStatusMsg = ds.Tables[0].Rows[0][1].ToString();
                }
             


            }

            return new JsonResult(LoginStatusMsg);

        }

on razor cshtml

&lt;span class=&quot;spn col-lg-2&quot;&gt;Account Status : @Model.LoginStatusMsg&lt;/span&gt;

答案1

得分: 1

Here is the translated code:

首先,不应将[HttpGet]属性应用于Razor页面处理程序方法,只需将其删除。

如果要调用OnGetChangeLoginStatus方法,您需要一个URL,如下所示:

var url = '/YourPageName?handler=ChangeLoginStatus&UserId=xxx';

在默认的Razor页面项目中,以下是一个最小示例,隐私页面:

<span class="spn col-lg-2">账户状态:@Model.LoginStatusMsg</span>

<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script>
    var turl = '/Privacy?handler=ChangeLoginStatus&UserId=2';
    $("body").click(function(){
        $.ajax({
            type: "GET",
            url: turl,

            success: function (data) {
                var message = "账户状态:" + data
                console.log(message)
                $("span").html(message)
            }
        });
    });
</script>

处理程序:

[BindProperty]
public string LoginStatusMsg { get; set; } = "Msg";

public void OnGet()
{
}

public JsonResult OnGetChangeLoginStatus(string UserId)
{
    return new JsonResult("NewMessage");
}

结果:

如何使用jQuery的AJAX请求来调用ChangeLoginStatus函数?

英文:

Firstly,[HttpGet] attribute shouldn't be applied to Razor Page handler method,just remove it

If you want to hit OnGetChangeLoginStatus method ,you need a url like:

 var url = &#39;/YourPageName?handler=ChangeLoginStatus&amp;UserId=xxx&#39;;

A minimal example in default razor page project ,Privacy Page:

&lt;span class=&quot;spn col-lg-2&quot;&gt;Account Status : @Model.LoginStatusMsg&lt;/span&gt;


&lt;script src=&quot;~/lib/jquery/dist/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
    var turl = &#39;/Privacy?handler=ChangeLoginStatus&amp;UserId=2&#39;;
    $(&quot;body&quot;).click(function(){
        $.ajax({
            type: &quot;GET&quot;,
            url: turl,

            success: function (data) {
                var message = &quot;Account Status :&quot; + data
                console.log(message)
                $(&quot;span&quot;).html(message)

            }
        });
    });
    
&lt;/script&gt;

Handler:

[BindProperty]
        public string LoginStatusMsg { get; set; } = &quot;Msg&quot;;
       
        

        public void OnGet()
        {
        }

        public JsonResult OnGetChangeLoginStatus(string UserId)
        {
            return new JsonResult(&quot;NewMessage&quot;);
        }

Result:

如何使用jQuery的AJAX请求来调用ChangeLoginStatus函数?

huangapple
  • 本文由 发表于 2023年6月12日 07:51:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定