如何在ASP.Net中显示成功消息
本文关键字:显示 成功 消息 Net ASP | 更新日期: 2023-09-27 18:16:46
我是一个初学者,正在学习ASP。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这样的东西吗?我是初学者。
添加一个属性,如ShowMessage
到您的模型,并设置它,如果重置成功。
将视图更改为
@if (model.ShowMessage)
{
<p class="alert alert-success"> <strong>Success! </strong>Your Password has been successfully changed.</p>
}
…
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>
}
我使用ValidationMessage
。
第一个在razor页面添加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");
}
});