如何在 MVC C# 中使用 MvcHtmlString 创建 Alert confrim Yes/No

How to create Alert confrim Yes/No with MvcHtmlString in MVC C#

以下代码通过 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 中显示一个警报,它是 Yes/No 和 returns 单击的按钮,因此 我可以使用该值在脚本中。

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

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

您可以找到有关给定 link

的示例和文档

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

site.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);
});