0

以下代码通过 MvcHtmlString 显示警告。

在 C# 中

public static MvcHtmlString ShowAlert(this HtmlHelper helper, string message)
{
     StringBuilder sb = new StringBuilder();

     sb.Append(@"$.confirm({" +
                      "title: false," +
                      "content: '" + message + "'," +
                      "type: 'dark'," +
                      "boxWidth: '45%'," +
                      "animation: 'RotateY',closeAnimation: 'RotateY',rtl: true," +
                      "typeAnimated: true," +
                      "buttons:" +
                      "{Close: {" +
                      "btnClass: 'btn-blue'} }});");

     return MvcHtmlString.Create(sb.ToString());
}

在脚本中

<script type="text/javascript">
        $('#bSubmit').click(event,
            function() {
                event.preventDefault();
                @Html.ShowAlert("Test");
            });
</script>

在此处输入图像描述

我想在MvcHtmlString中显示一个 AlertYes/No并返回单击的按钮,以便我可以使用脚本中的值。

4

2 回答 2

0

我建议你使用 alertify https://alertifyjs.com/,它有自己的功能来创建你所要求的,它很容易使用,你只需要写一个新的 MvcHtmlString 写这个:

alertify.confirm('Confirm Title', 'Confirm Message', function(){ alertify.success('Ok') }
                , function(){ alertify.error('Cancel')});

您可以在给定的链接上找到示例和文档

于 2020-03-29T09:37:59.357 回答
0

您可以在应用程序中创建一个常用函数的 javascript 库,并将确认对话框包含在其中。这样,您可以使用脚本中确认对话框的返回值作为回调。

网站.js:

function showConfirm(message, clickCallback) {
    $.confirm({
        title: false,
        content: message,
        type: 'dark',
        boxWidth: '45%',
        animation: 'RotateY',
        closeAnimation: 'RotateY', 
        rtl: true,
        typeAnimated: true,
        buttons: { 
            Close: function () {
                clickCallback("Close");
            }
        } 
    });
}

按钮的名称可以作为另一个参数放入函数中,例如:

function showConfirmWithMultipleButtons(message, buttons /* array of names */, clickCallback)

用法:

showConfirm('Simple confirm message', function(buttonName) {
    $.alert(buttonName);
});
于 2020-03-29T09:39:53.280 回答