Razor 页面在异步后从代码调用模式

问题描述

亲爱的

我确实读取了条形码并在读取代码后通过回发将用户保存在数据库中,而在 OnPostAsync 中,我对用户进行了验证,如果用户续订日期已过期,我想显示一个弹出窗口?那我该怎么做?

解决方法

您可以使用ajax调用OnPostAsync,然后在ajax成功时显示一个弹窗。这是一个演示:

cshtml(你可以把你想传入的数据放到ajax的data中):

@Html.AntiForgeryToken()
<button onclick="postdata()">submit</button>
@section Scripts
{
    <script>
        function postdata() {
            $.ajax({
                type: "POST",url: '',headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },data: { id: 1 }
            }).done(function (result) {
                if (result.message != "") {
                    alert("message:"+result.message);
                    }
                    
                });
                
        }
    </script>

}

cshtml.cs:

public async Task<IActionResult> OnPostAsync(int id)
        {
            //you can check if the user renewal date is expired here 
            if (id==1)
            {
                return new JsonResult(new{ message = "user renewal date is expired" });
            }
            else {
                return new JsonResult(new { message = "" });
            }
        }

结果: enter image description here