英文:
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 > 0)
{
if (ds.Tables[0].Rows[0][0].ToString() != "0")
{
LoginStatusMsg = ds.Tables[0].Rows[0][1].ToString();
}
}
return new JsonResult(LoginStatusMsg);
}
on razor cshtml
<span class="spn col-lg-2">Account Status : @Model.LoginStatusMsg</span>
答案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");
}
结果:
英文:
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 = '/YourPageName?handler=ChangeLoginStatus&UserId=xxx';
A minimal example in default razor page project ,Privacy Page:
<span class="spn col-lg-2">Account Status : @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 = "Account Status :" + data
console.log(message)
$("span").html(message)
}
});
});
</script>
Handler:
[BindProperty]
public string LoginStatusMsg { get; set; } = "Msg";
public void OnGet()
{
}
public JsonResult OnGetChangeLoginStatus(string UserId)
{
return new JsonResult("NewMessage");
}
Result:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论