ASP.NET MVC多页表单:验证无效

时间:2015-08-19 16:53:47

标签: asp.net-mvc validation

我正在asp mvc中创建一个注册登录页面,正如我所知,这个页面有两个模型和两个表单动作。 一切都好,但验证。 模型是:

public class Account_Index_ViewModel
{
    public UserAccount_Login_ViewModel userAccount_Login_ViewModel { get; set; }
    public UserAccount_Register_ViewModel userAccount_Register_ViewModel { get; set; }
}

public class UserAccount_Login_ViewModel
{
    [Required]
    [DataType(DataType.Password)]
    public string Pass { get; set; }
    [Required]
    public string LoginName { get; set; } // NickName/Email/MobilePhone
}

public class UserAccount_Register_ViewModel
{
    public string NickName { get; set; }
    public string Passw { get; set; }
    public string PassConfirm { get; set; }
    public string Email { get; set; }
    public string MobilePhone { get; set; }
}

和视图:

@model GhafasehWebSite.Models.Account_Index_ViewModel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<div class="AccountBook">
    <div class="half-width">
        @using (Html.BeginForm("Login", "Account"))
        {
            Html.EnableClientValidation();
            @Html.AntiForgeryToken()

            <div class="form-horizontal">
                <h4>ورود به سیستم</h4>
                <hr />
                @Html.ValidationSummary(true)

                <div class="form-group col-md-12">
                    @Html.LabelFor(model => ((GhafasehWebAPI.Models.UserAccount_Login_ViewModel)(model.userAccount_Login_ViewModel)).LoginName, new { @class = "control-label col-md-4" })
                    <div class="col-md-8">
                        @Html.TextBoxFor(model => ((GhafasehWebAPI.Models.UserAccount_Login_ViewModel)(model.userAccount_Login_ViewModel)).LoginName, new { @class = "form-control", placeholder = "نام مستعار/ایمیل/شماره موبایل" })
                        @Html.ValidationMessageFor(model => ((GhafasehWebAPI.Models.UserAccount_Login_ViewModel)(model.userAccount_Login_ViewModel)).LoginName)
                    </div>
                </div>

                <div class="form-group col-md-12">
                    @Html.LabelFor(model => ((GhafasehWebAPI.Models.UserAccount_Login_ViewModel)(model.userAccount_Login_ViewModel)).Pass, new { @class = "control-label col-md-4" })
                    <div class="col-md-8">
                        @Html.TextBoxFor(model => ((GhafasehWebAPI.Models.UserAccount_Login_ViewModel)(model.userAccount_Login_ViewModel)).Pass, new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => ((GhafasehWebAPI.Models.UserAccount_Login_ViewModel)(model.userAccount_Login_ViewModel)).Pass)
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-md-offset-4 col-md-10">
                        <input type="submit" value="ورود" class="btn btn-primary" />
                    </div>
                </div>
            </div>
        }
    </div>
    <div class="half-width">
        @using (Html.BeginForm("Register","Account"))
        {
            @Html.AntiForgeryToken()

            <div class="form-horizontal">
                <h4>ثبت نام در سیستم</h4>
                <hr />
                @Html.ValidationSummary(true)

                <div class="form-group col-md-12">
                    @Html.LabelFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).NickName, new { @class = "control-label  col-md-4" })
                    <div class=" col-md-8">
                        @Html.TextBoxFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).NickName, new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).NickName)
                    </div>
                </div>

                <div class="form-group col-md-12">
                    @Html.LabelFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).Passw, new { @class = "control-label  col-md-4" })
                    <div class=" col-md-8">
                        @Html.TextBoxFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).Passw, new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).Passw)
                    </div>
                </div>

                <div class="form-group col-md-12">
                    @Html.LabelFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).PassConfirm, new { @class = "control-label  col-md-4" })
                    <div class=" col-md-8">
                        @Html.TextBoxFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).PassConfirm, new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).PassConfirm)
                    </div>
                </div>

                <div class="form-group col-md-12">
                    @Html.LabelFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).Email, new { @class = "control-label  col-md-4" })
                    <div class=" col-md-8">
                        @Html.TextBoxFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).Email, new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).Email)
                    </div>
                </div>

                <div class="form-group col-md-12">
                    @Html.LabelFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).MobilePhone, new { @class = "control-label  col-md-4" })
                    <div class=" col-md-8">
                        @Html.TextBoxFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).MobilePhone, new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => ((GhafasehWebAPI.Models.UserAccount_Register_ViewModel)model.userAccount_Register_ViewModel).MobilePhone)
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-md-offset-4 col-md-10">
                        <input type="submit" value="ثبت نام" class="btn btn-success" />
                    </div>
                </div>
            </div>
        }
    </div>
</div>

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

和控制器:

    [HttpGet]
    public ActionResult Index()
    {
        return View(new Account_Index_ViewModel());
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Login(UserAccount_Login_ViewModel model)
    {
        if (ModelState.IsValid)
        {
            if (DataProvider.LoginUser(model, ModelState, Request, Session))
            {
                return RedirectToAction("Index", "Home");
            }
        }
        return View("Index");
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Register(UserAccount_Register_ViewModel model)
    {
        if (ModelState.IsValid)
        {
            if (DataProvider.RegisterUser(model, ModelState, Request, Session))
            {
                return RedirectToAction("Index", "Home");
            }
        }
        return View("Index");
    }

你应该知道服务器端验证工作正常,但客户端是睡着了。 所以,你有什么建议?

1 个答案:

答案 0 :(得分:1)

当你说验证不起作用时,你的意思是什么?你用空的用户名/密码按登录按钮,它没有显示所需的错误? 如果是这样,我用您的模型视图控制器创建了新项目,它工作了!!!验证工作。