如何使用 aspnet Razor 显示弹出消息



我需要验证表单中的某些字段,我没有使用任何前端语言来执行此操作,我正在从我需要显示一些消息ActionResult获取信息!

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ResetPassword(string user, string hash)
{
string old = Request.Form["oldpassword"];
string password = Request.Form["password"];
string repeat = Request.Form["repeatpassword"];

if(String.IsNullOrEmpty(old) || String.IsNullOrEmpty(password) || String.IsNullOrEmpty(repeat))
{
"show a dialog".
}

这是一个好方法:

视图:

@model Testy20161006.Controllers.ResetPasswordViewModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>IndexPage2</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
@{
if (ViewBag.ShowDialog != null && ViewBag.ShowDialog == true)
{
<script type="text/javascript">
$(function () {
$("#aModal").modal('show');
})
</script>
}
}
@using (Html.BeginForm("ResetPassword", "Home"))
{
<div>
<table>
<tr>
<td>@Html.LabelFor(r => r.oldPassword)</td>
<td>@Html.TextBoxFor(r => r.oldPassword)</td>
</tr>
<tr>
<td>@Html.LabelFor(r => r.newPassword)</td>
<td>@Html.TextBoxFor(r => r.newPassword)</td>
</tr>
<tr>
<td>@Html.LabelFor(r => r.repeatNewPassword)</td>
<td>@Html.TextBoxFor(r => r.repeatNewPassword)</td>
</tr>
</table>
</div>
<input type="submit" value="submit" />
}
@*modal*@
<div class="modal fade" id="aModal" tabindex="-1" role="dialog" aria-labelledby="aModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="aModalLabel">Warning</h4>
</div>
<div class="modal-body">
Old Password, New Password, or repeat Password is empty.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</body>
</html>

控制器/视图型号:

public class ResetPasswordViewModel
{
public string oldPassword { get; set; }
public string newPassword { get; set; }
public string repeatNewPassword { get; set; }
}
public class HomeController : Controller
{
[HttpPost]
public ActionResult ResetPassword(ResetPasswordViewModel resetPasswordViewModel)
{
if (String.IsNullOrEmpty(resetPasswordViewModel.oldPassword) ||
String.IsNullOrEmpty(resetPasswordViewModel.newPassword) ||
String.IsNullOrEmpty(resetPasswordViewModel.repeatNewPassword)
)
{
//show dialog
ViewBag.ShowDialog = true;
}
return View("IndexPage2", resetPasswordViewModel);
}
public ActionResult IndexPage2()
{   //start page specified in routeconfig.cs
return View();
}

最新更新