微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Asp.net Core Razor页面中的自举警报

如何解决Asp.net Core Razor页面中的自举警报

如何在asp.net核心剃须刀页面中配置引导警报。

 <div class="alert alert-success">
   <strong>Well done!</strong> You successfully read this important alert message.
 </div>

解决方法

据我所知,如果要显示警报,可以尝试使用jquery来满足您的要求,可以将类添加到警报div中。您可以使用ajax调用razor页面的onpost方法并检查响应,如果响应为true,则可以警告成功,否则可以警告失败消息。

更多详细信息,您可以参考以下示例代码:

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

@{

    Layout = null;

}


<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

<div class="alert alert-success alert-dismissible fade" id="buttonAlertSuccess">
    <strong>Well done!</strong> You successfully read this important alert message.
</div>
<div class="alert alert-success alert-dismissible fade" id="buttonAlertFail">
    <strong>False</strong> You failed read this important alert message.
</div>
<button class="btn btn-secondary" id="modalButton" type="submit">Button</button>
@Html.AntiForgeryToken()

<script>
    $(function () {

        $("#modalButton").click(function () {

            $.ajax({
                type: "post",url: "index",beforeSend: function (xhr) {
                    xhr.setRequestHeader("CSRF-TOKEN",$('input:hidden[name="__RequestVerificationToken"]').val());
                },success: function (response) {
                    if (response == "True") {
                        $("#buttonAlertSuccess").addClass('in');
                    } else {
                        $("#buttonAlertFail").addClass('in');
                    }
                }

            });

        })
    });
</script>

发布方法:

    public ActionResult OnPost() {

        return new JsonResult("True");
    }

注意:

如果要使用ajax调用,则应在startup.cs ConfigureServices方法中设置CSRF-TOKEN设置,如下所示:

        services.AddAntiforgery(o => o.HeaderName = "CSRF-TOKEN");

结果:

enter image description here

,

请找到完整的代码并尝试使用

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div id="alert_div" style="margin: 0 0.5%; -webkit-box-shadow: 3px 4px 6px #999;" class="alert-success">
<strong>Well done!</strong> You successfully read this important alert message.
 </div>
</body>
</html>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?