如何在ASP.Net中显示成功消息

时间:2016-11-07 18:07:05

标签: c# asp.net asp.net-mvc razor asp.net-mvc-5

我是初学者并且正在学习ASP.Net MVC Web开发。我被困在一个地方。

我有一个重置密码窗口。如果用户成功重置密码,我希望他重定向到同一页面,但我想要一个"密码重置成功"消息在顶部。它可以是警报或其他东西。只是用户应该知道密码已被重置。但我不知道如何实现这一目标。以下就是我现在所拥有的。

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ViewResult WidgetResetPassword(ResetPasswordMV data)
    {
        bool isValid = false;

        isValid = ModelState.IsValid;

        // verify the 2 password match
        if (isValid && !data.NewPassword.Equals(data.ConfirmNewPassword))
        {
            isValid = false;
            ModelState.AddModelError("ConfirmNewPassword", "Passwords doesn't match.");
        }

        if (isValid)
        {
            //setting the correct tenant id
            if (Session[Constants.SESSION_USER_KEY] != null)
            {
                UserMV authenticatedUsers = (UserMV)Session[Constants.SESSION_USER_KEY];

                data.UserId = authenticatedUsers.Id;
            }

            ResetPasswordHelper.Save(data);
        }
        return View(data);
    }

我的.cshtml文件:

@model ResetPasswordMV
@using SIM_Obj.ModelViews;

@{
    ViewBag.Title = "Reset Password";
}

@using (Html.BeginForm())
{
        @Html.AntiForgeryToken()

@*    <div id="ResetPasswordWidget" class="form-horizontal">*@
        <div id="ResetPasswordWidget" class="form-horizontal-width-inherit">
            <h3>Reset Password</h3>
            <hr/>

            @Html.ValidationSummary(true, "", new {@class = "text-danger"})

            @Html.HiddenFor(model => model.UserId)

            <div class="form-group">
                @Html.LabelFor(model => model.OldPassword, htmlAttributes: new {@class = "control-label col-md-2"})
                <div class="col-md-10">
                    @Html.EditorFor(model => model.OldPassword, new {htmlAttributes = new {@class = "form-control"}})
                    @Html.ValidationMessageFor(model => model.OldPassword, "", new {@class = "text-danger"})
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.NewPassword, htmlAttributes: new {@class = "control-label col-md-2"})
                <div class="col-md-10">
                    @Html.EditorFor(model => model.NewPassword, new {htmlAttributes = new {@class = "form-control"}})
                    @Html.ValidationMessageFor(model => model.NewPassword, "", new {@class = "text-danger"})
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.ConfirmNewPassword, htmlAttributes: new {@class = "control-label col-md-2"})
                <div class="col-md-10">
                    @Html.EditorFor(model => model.ConfirmNewPassword, new {htmlAttributes = new {@class = "form-control"}})
                    @Html.ValidationMessageFor(model => model.ConfirmNewPassword, "", new {@class = "text-danger"})
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Reset Password" class="btn btn-default"/>
                </div>
            </div>
        </div>
    }

我希望能够添加以下内容:

<p class ="alert alert-success"> <strong>Success! </strong>Your Password has been successfully changed.</p>

问题1:重设密码时如何显示

问题2:目前我正在控制器中进行return view(data)。是否有必要。

问题3:我可以使用像partialview这样的东西。我是初学者。请指导我。

3 个答案:

答案 0 :(得分:1)

向您的模型添加ShowMessage等属性,并在重置成功时进行设置。

将视图更改为

@if (model.ShowMessage)
{
    <p class="alert alert-success"> <strong>Success! </strong>Your Password has been successfully changed.</p>
}

答案 1 :(得分:0)

...

ViewBag.SaveResult=true;
return View(data);

..... 在视图页面

@if (ViewBag.SaveResult == true) {
<div class="alert alert-success" >             
<strong>Success! </strong>Your Password has been successfully changed.</div>


}

答案 2 :(得分:0)

我使用ValidationMessage完成了此操作。

首先在剃刀页面上添加V alidationMessage,以获取成功和错误:

@Html.ValidationMessage("ERROR", new { @class = "card-alert alert alert-danger v-message", @style = "display: none;" })
@Html.ValidationMessage("SUCCESS", new { @class = "card-alert alert alert-success v-message", @style = "display: none;" })

然后在控制器中,您使用以下命令发送错误和成功消息:

ModelState.AddModelError("SUCCESS", "Success message !");
ModelState.AddModelError("ERROR", "Error message !");

在JavaScript中添加以下代码女巫将在需要时显示消息:

$(".validation-message").each(function () {
    if ($(this).text() !== "") {
        $(this).css("display", "block");
    }
});