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

C# 如何使用aspnet Razor显示弹出消息,c#,asp.net-mvc,C#,Asp.net Mvc,我需要验证表单中的一些字段,我没有使用任何前端语言来执行此操作,我从ActionResult中获取信息,我需要显示一些消息 [HttpPost] [ValidateAntiForgeryToken] public ActionResult ResetPassword(string user, string hash) { string old = Request.Form["oldpassword"]; string passwor

我需要验证表单中的一些字段,我没有使用任何前端语言来执行此操作,我从
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".
        }

下面是一个很好的方法:

视图:


您可以将消息值传递给JS模式弹出窗口:。只需从viewmodel中设置基于模式的弹出文本,例如,
$(“.modal body”).text(“@Model.Message”)
。如果您发表了一篇正常文章,则添加一个
ModelStateError
,并返回视图,使其在
@Html.ValidationSummary()
占位符中为sisplayef。
ModelState.AddModelError
方法也有效,至少在适当的位置使用
ValidationSummary
ValidationMessage
$(“.modal body”).text(“@Html.ValidationSummary()”)
。它不使用
@Html.EditorFor
?我在用
谢谢大家!很好
ModelState.AddModelError
假设我需要验证更多的东西?
ViewBag.ShowDialog=true将显示cshtml内部的逻辑,如何区分?更多的viewbags将起作用。最好的方法是将[required]属性放在视图模型中所需的属性上。在操作中,检查ModelState.Valid,如果有效,则写入db或重置密码,否则再次显示视图。视图将自动指出哪些字段不符合验证要求。Html.ValidationSummary(false)将显示所有错误,Html.ValidationMessageFor(model=>model.FieldName)将在字段旁边放置一个错误。明白了!谢谢
@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();
    }