一页中的ASP.NET MVC多个表单:验证不起作用 [英] ASP.NET MVC Multiple form in one page: Validation doesn't work

查看:40
本文介绍了一页中的ASP.NET MVC多个表单:验证不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在asp mvc中创建一个注册登录页面,并且根据需要,该页面具有两个模型和两个表单操作. 每件事都可以,但可以通过验证. 这些模型是:

I am creating a register login page in asp mvc and as i need, this page has two models and two form actions. every thing is ok but the validation. the models are:

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

您应该知道服务器端验证工作正常,但客户端处于睡眠状态. 那么,你有什么建议?

you should know that server side validation works fine but the client side is asleep. So, what do you suggest?

推荐答案

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

When you say the validation doesn't work what do you mean? you press Login button with empty username/password and it doesn't show required error? If so , I Created new project with your model view controller and it worked !!! The validation works.

这篇关于一页中的ASP.NET MVC多个表单:验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆