简体   繁体   English

如何在 swal 消息中使用@Html.Raw

[英]How to use @Html.Raw in swal message

I want to pass controller html msg to view swal pop-up box but it's showing html code not html formated msg - like this:我想通过控制器 html msg 来查看 swal 弹出框,但它显示的是 html 代码而不是 html 格式的 msg - 如下所示:

这是输出

Controller code:控制器代码:

ViewBag.message = "<h3> Something went terribly wrong, please try again or contact us at <a href='mailto:info@abc.we'>info@abc.we</a> </h3>";

View markup:查看标记:

@{
    var msg = Html.Raw(ViewBag.message);
}

<script type="text/javascript">
$(function () {
                swal({
                    title: "@Html.Raw(msg)",
                    text: "",
                    icon: "success",
                    button: "Ok",
                });
            });
</script>

This is my code - can anyone please help me find a solution?这是我的代码 - 谁能帮我找到解决方案? I am stuck on this problem.我被困在这个问题上。

I just want controller html msg to be displayed properly in swal pop-up.我只想在 swal 弹出窗口中正确显示控制器 html 消息。

Thanks in advance to whoever is able to help me with this problem.提前感谢能够帮助我解决这个问题的人。

Another Example:另一个例子:

Controller :控制器

ViewBag.htmlmsg = "alert('asd');"; ViewBag.htmlmsg = "alert('asd');";

View :查看:

<script>
    $(document).ready(function () {
    @Html.Raw(ViewBag.htmlmsg);
        
});
</script>

this one is also not working.这个也不起作用。 not showing alert pop-up.不显示警报弹出窗口。

I just want controller html msg to be displayed properly in swal pop-up.我只想在 swal 弹出窗口中正确显示控制器 html 消息。

Well, swal alert has the content attribute to allow htmlContent you can use that as well.好吧, swal alert 具有允许htmlContentcontent属性,您也可以使用它。 In that case you could set additional span and bind your viewbag there, which will generate expected result.在这种情况下,您可以设置额外的span并在那里绑定您的viewbag ,这将产生预期的结果。

Controller:控制器:

public class RawHtmlController : Controller
    {
        public IActionResult Index()
        {
           
            ViewBag.message = "<h3> Something went terribly wrong, please try again or contact us at <a href='mailto:info@abc.we'>info@abc.we</a> </h3>";
            return View();
        }
    }

Script:脚本:

@{
   

}


@section scripts {
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
    <script>

        $(document).ready(function () {
           
            var message = "@Html.Raw(ViewBag.message)";

            var span = document.createElement("span");
            span.innerHTML = message;
           

            $(function () {
                swal({
                    content: span,
                    icon: "success",
                    button: "Ok",
                });
            });
        });
    </script>
        }

Your Scenario:你的场景:

    <script type="text/javascript">

                var message = "@Html.Raw(ViewBag.message)";
    
                var span = document.createElement("span");
                span.innerHTML = message;
                   $(function () {
                    swal({
                        content: span,
                        icon: "success",
                        button: "Ok",
                    });
                });
    </script>

Output:输出:

在此处输入图像描述

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

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