简体   繁体   English

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

[英]How to show a popup message with aspnet Razor

I need to validate some fields from a form, I'm not using any front-end language to perform this, I'm getting the information from the ActionResult I need to show some message! 我需要验证表单中的某些字段,我没有使用任何前端语言来执行此操作,而是从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".
        }

Here is a good way to do it: 这是一个很好的方法:

View: 视图:

@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>

Controller/ViewModel: 控制器/ ViewModel:

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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM