Asp.net mvc 除电子邮件验证外,MVC4表单验证不起作用

Asp.net mvc 除电子邮件验证外,MVC4表单验证不起作用,asp.net-mvc,Asp.net Mvc,我正在构建一个MVC4应用程序,但是除了电子邮件ID之外,下面的表单验证都不起作用。不满足最低要求的用户名和密码仍可注册。即使密码和确认密码不匹配,注册也会失败。有人能帮忙吗 using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Web; namespace ReadingCat.Models { public class User { public

我正在构建一个MVC4应用程序,但是除了电子邮件ID之外,下面的表单验证都不起作用。不满足最低要求的用户名和密码仍可注册。即使密码和确认密码不匹配,注册也会失败。有人能帮忙吗

 using System.Collections.Generic;
 using System.ComponentModel.DataAnnotations;
 using System.Web;

namespace ReadingCat.Models
{

public class User
{
    public User()
    {
        File = new List<HttpPostedFileBase>();

    }
    public int userid { get; set; }
    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength 
    = 6)]
    public string username { get; set; }

    [Required]
    [EmailAddress(ErrorMessage = "Please enter a valid email ID")]
    public string useremail { get; set; }
    public string bio { get; set; }


    [Required]
    [StringLength(10, MinimumLength = 3)]
    [DataType(DataType.Password)]
    [Display(Name = "password")]
    public string password { get; set; }


    [DataType(DataType.Password)]
    [Compare("password", ErrorMessage = "The password and confirm password do not match")]
    public string confirmPassword { get; set; }
    public int isAdmin { get; set; }
    public List<HttpPostedFileBase> File { get; set; }
    public string paths { get; set; }
}


}
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Web;
名称空间ReadingCat.Models
{
公共类用户
{
公共用户()
{
文件=新列表();
}
public int userid{get;set;}
[必需]
[StringLength(100,ErrorMessage={0}必须至少有{2}个字符长。”,MinimumLength
= 6)]
公共字符串用户名{get;set;}
[必需]
[电子邮件地址(ErrorMessage=“请输入有效的电子邮件ID”)]
公共字符串useremail{get;set;}
公共字符串bio{get;set;}
[必需]
[StringLength(10,最小长度=3)]
[数据类型(数据类型.密码)]
[显示(Name=“密码”)]
公共字符串密码{get;set;}
[数据类型(数据类型.密码)]
[比较(“密码”,ErrorMessage=“密码与确认密码不匹配”)]
公共字符串confirmPassword{get;set;}
public int isAdmin{get;set;}
公共列表文件{get;set;}
公共字符串路径{get;set;}
}
}
这是查看代码

@model ReadingCat.Models.User

@{
  Layout = null;
 }

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Register1</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Sign Up Form by Colorlib</title>

<!-- Font Icon -->
<link rel="stylesheet" href="fonts/material-icon/css/material-design-iconic- 
 font.min.css">

<!-- Main css -->
<link rel="stylesheet" href="~/css/register.css">

</head>
<body>
<div class="main">

    <div class="container">
        <div class="book-content">
            <div class="book-image">
                <img class="book-img" src="~/images/register.gif" alt="">
            </div>
            <div class="book-form">
                @using (Html.BeginForm())
                {
                    @Html.AntiForgeryToken()


                    <form id="book-form">
                        <h2>Register Now!</h2>
                        <hr />
                        @Html.ValidationSummary(true, "", new { @class = 
  "text-danger" })
                        <div class="form-group form-input">
                            @Html.LabelFor(model => model.username, 
htmlAttributes: new { @class = "register.css" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.username, new 
{ htmlAttributes = new { @class = "register.css" } })
                                @Html.ValidationMessageFor(model => 
model.username, "", new { @class = "text-danger" })
                            </div>
                        </div>

                        <div class="form-group form-input">
                            @Html.LabelFor(model => model.useremail, 
htmlAttributes: new { @class = "register.css" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model.useremail, 
new { htmlAttributes = new { @class = "register.css" } })
                                @Html.ValidationMessageFor(model => 
model.useremail, "", new { @class = "text-danger" })
                            </div>
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.password, 
htmlAttributes: new { @class = "register.css" })
                            <div class="col-md-10">
                                @Html.PasswordFor(model => model.password, 
new { htmlAttributes = new { @class = "register.css" } })
                                @Html.ValidationMessageFor(model => 
model.password, "", new { @class = "text-danger" })
                            </div>
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.confirmPassword, 
htmlAttributes: new { @class = "register.css" })
                            <div class="col-md-10">
                                @Html.PasswordFor(model => 
model.confirmPassword, new { htmlAttributes = new { @class = "register.css" 
} })
                                @Html.ValidationMessageFor(model => 
model.confirmPassword, "", new { @class = "text-danger" })
                            </div>
                        </div>

                        <br />
                        <div class="form-submit">
                            <input type="submit" value="Submit" 
class="submit" id="submit" name="submit" />
                        </div>
                    </form>
                }

            </div>
        </div>
    </div>
</div>


<!-- JS -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="js/main.js"></script>
</body>
 </html>
@model ReadingCat.Models.User
@{
布局=空;
}
登记员1
通过Colorlib注册表格
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
现在注册!

@Html.ValidationSummary(true,“,新{@class= “文本危险”}) @Html.LabelFor(model=>model.username, htmlAttributes:new{@class=“register.css”}) @EditorFor(model=>model.username,新建 {htmlAttributes=new{@class=“register.css”}) @Html.ValidationMessageFor(模型=> model.username,“,新{@class=“text danger”}) @Html.LabelFor(model=>model.useremail, htmlAttributes:new{@class=“register.css”}) @Html.EditorFor(model=>model.useremail, new{htmlAttributes=new{@class=“register.css”}) @Html.ValidationMessageFor(模型=> model.useremail,“,新{@class=“text danger”}) @LabelFor(model=>model.password, htmlAttributes:new{@class=“register.css”}) @Html.PasswordFor(model=>model.password, new{htmlAttributes=new{@class=“register.css”}) @Html.ValidationMessageFor(模型=> model.password,“,新{@class=“text danger”}) @Html.LabelFor(model=>model.confirmPassword, htmlAttributes:new{@class=“register.css”}) @Html.PasswordFor(模型=> model.confirmPassword,new{htmlAttributes=new{@class=“register.css” } }) @Html.ValidationMessageFor(模型=> model.confirmPassword,“,新{@class=“text danger”})
}
我检查了您的代码,发现您缺少两个必需的js文件。 请添加以下文件,它应该工作

<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>


您可以使用下面的链接查看实况演示


值得指出两件事;1) 不要完全依赖客户端验证——您仍然需要在控制器中验证表单。2) 在可能的情况下,最好遵循这个规则-在这种情况下,这意味着您的模型的属性应该是PascalCase。