C# 使用SweetAlert返回错误消息

C# 使用SweetAlert返回错误消息,c#,viewbag,sweetalert,asp.net-boilerplate,C#,Viewbag,Sweetalert,Asp.net Boilerplate,在AccountController中,我注意到示例注册代码捕获UserFriendlyException,并在ViewBag中返回错误消息 我如何从SweetAlert返回它 [HttpPost] public virtual async Task<ActionResult> Register(RegisterViewModel model) { try { // Code omitted for brevity } catch (U

AccountController
中,我注意到示例注册代码捕获
UserFriendlyException
,并在
ViewBag
中返回错误消息

我如何从SweetAlert返回它

[HttpPost]
public virtual async Task<ActionResult> Register(RegisterViewModel model)
{
    try
    {
        // Code omitted for brevity
    }
    catch (UserFriendlyException ex)
    {
        ViewBag.ErrorMessage = ex.Message; // I need to return this using SweetAlert

        return View("Register", model);
    }
}

由于该方法返回一个
View
结果,因此使用
ViewBag
作为错误消息是有意义的

要显示SweetAlert,请在中的脚本部分添加以下内容:

@节脚本{
// ...
@如果(ViewBag.ErrorMessage!=null)
{
abp.message.error(“@ViewBag.ErrorMessage”);
/*swal(“@ViewBag.ErrorMessage”,“error”)*/
}
}

两个
标记都会触发相同的弹出窗口。

我知道该示例没有脚本,但我希望以与登录页面相同的方式创建该页面
  <form action="javascript:;" id="register-form" class="login-form" method="post">
        <div class="alert alert-danger display-hide">
            <button class="close" data-close="alert"></button>
            <span>Enter required fields. </span>
        </div>

        @if (@ViewBag.ErrorMessage != null)
        {
            <div class="alert alert-danger">
                <i class="fa fa-warning"></i> @ViewBag.ErrorMessage
            </div>
           <script>abp.message.error("@ViewBag.ErrorMessage");</script>
            <input type="hidden" value=" @ViewBag.ErrorMessage" id="hf_error" >
        }



        <div class="row">
            <div class="col-xs-12">

                <input type="text" class="form-control form-control-solid placeholder-no-fix form-group" autocomplete="off" name="name" placeholder="@L("Name")" required autofocus id="name">
            </div>
            <div class="col-xs-12">
                <input class="form-control form-control-solid placeholder-no-fix form-group" type="text" autocomplete="off" placeholder="@L("Surname")" name="surname" required id="surname"  />
            </div>

            <div class="col-xs-12">

                <input type="password" class="form-control form-control-solid placeholder-no-fix form-group" autocomplete="off" name="password" placeholder="@L("Password")" required autofocus id="password">
            </div>

        </div>
        <div class="row">
            <div class="col-sm-6 text-left">
                <div class="forgot-password" style="margin-top: 5px;">
                    <a href="@Url.Action("Login")" id="register-btnd" class="forget-password">Login To Your Account</a>
                </div>
            </div>
            <div class="col-sm-6 text-right">
                <button class="btn green" id="btnSubmit" type="submit">Register</button>
            </div>
            <hr />
        </div>
    </form>
 var jsonObject = {
            Name: name,
            Surname: surname,
            //EmailAddress: email,
           // UserName: username,
            Password: password
        };


        abp.ajax({
            url: abp.appPath + 'Account/Register',
            type: 'POST',
            data: JSON.stringify(jsonObject)
        }).done(function(data) {

            alert("done");


        }).fail(function(data) {

           alert("fail");
        });