C# 数据批注验证在ASP.net中不起作用
我正在尝试实现数据注释。我创建了一个名为RegisterViewModel的视图模型,其中包含以下数据注释C# 数据批注验证在ASP.net中不起作用,c#,asp.net,asp.net-mvc,asp.net-mvc-4,data-annotations,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Data Annotations,我正在尝试实现数据注释。我创建了一个名为RegisterViewModel的视图模型,其中包含以下数据注释 namespace CSBSTest.ViewModel{public class RegisterViewModel { [Required(ErrorMessage = "Filed is required")] public string Name { get; set; } [Required(ErrorMessage ="Filed is require
namespace CSBSTest.ViewModel{public class RegisterViewModel
{
[Required(ErrorMessage = "Filed is required")]
public string Name { get; set; }
[Required(ErrorMessage ="Filed is required")]
[StringLength(9,ErrorMessage ="The Registeration number is not valid")]
[RegularExpression("BCS15[1-9][1-9][1-9]",ErrorMessage ="The CUST reg number is not valid")]
public string RegNo { get; set; }
[Required(ErrorMessage = "Filed is required")]
[EmailAddress]
public string Email { get; set; }
[Required(ErrorMessage = "Filed is required")]
[MaxLength(11)]
public string Phone { get; set; }
[Required]
[MinLength(6,ErrorMessage ="Minimun length must be 6 charccters")]
public string Password { get; set; }
[Required]
[Compare("Password")]
public string ConfirmPassword { get; set; }
}
}
然后,我为RegisterViewModel创建了一个表单,如下所示:
@model CSBSTest.ViewModel.RegisterViewModel@{
ViewBag.Title = "SignupForm";
Layout = "~/Views/Shared/_NavBar.cshtml";}
<head>
<link href="~/Content/Site.css" rel="stylesheet" /></head>
<h2 id="head">
Let's get Started
</h2>
<form action="~/Home/SignUp" method="post" style="padding:70px">
<div class="form-row">
<div class="form-group col-md-3">
<label for="inputEmail4">Full Name</label>
<input type="text" class="form-control" id="inputEmail4" placeholder="Muhammad Ali" @Html.TextBoxFor(x => x.Name)
@Html.ValidationMessageFor(x => x.Name)
</div>
<div class="form-group col-md-3">
<label for="inputPassword4">Email</label>
<input type="email" class="form-control" id="inputPassword4" placeholder="EaF@gmail.com" @Html.TextBoxFor(x => x.Email)
@Html.ValidationMessageFor(x => x.Email)
</div>
</div>
<div class="form-row">
<div class="form-group col-md-3">
<label for="inputEmail4">Password</label>
<input type="password" class="form-control" @Html.TextBoxFor(x => x.Password)
@Html.ValidationMessageFor(x => x.Password)
</div>
<div class="form-group col-md-3">
<label for="inputAddress">Confirm Password</label>
<input type="password" class="form-control" id="inputAddress" @Html.TextBoxFor(x => x.ConfirmPassword)
@Html.ValidationMessageFor(x => x.ConfirmPassword)
</div>
</div>
<div class="form-row">
<div class="form-group col-md-3">
<label for="inputAddress2">Cust Reg No</label>
<input type="datetime" class="form-control" id="inputAddress2" placeholder="20 May 1996" @Html.TextBoxFor(x => x.RegNo)
@Html.ValidationMessageFor(x => x.RegNo)
</div>
<div class="form-group col-md-3">
<label for="inputAddress2">Phone Number</label>
<input type="number" class="form-control" id="inputAddress2" placeholder="+92-3245009538" @Html.TextBoxFor(x => x.Phone)
@Html.ValidationMessageFor(x => x.Phone)
</div>
</div>
<div class="form-row">
<div class="form-group col-md-3">
@Html.Label("Dept", "Select Department")
@Html.DropDownList("Department", new SelectList(new[] { "IT", "CS" }), new { @class = "form-control" })
</div>
</div>
<br />
<button id="bt" type="submit" class="btn btn-primary">Register</button>
</form>
@model CSBSTest.ViewModel.RegisterViewModel@{
ViewBag.Title=“SignupForm”;
Layout=“~/Views/Shared/\u NavBar.cshtml”;}
让我们开始吧
全名
x、 (姓名)
@Html.ValidationMessageFor(x=>x.Name)
电子邮件
x、 (电邮)
@Html.ValidationMessageFor(x=>x.Email)
密码
x、 密码)
@Html.ValidationMessageFor(x=>x.Password)
确认密码
x、 确认密码)
@Html.ValidationMessageFor(x=>x.ConfirmPassword)
客户注册号
x、 RegNo)
@Html.ValidationMessageFor(x=>x.RegNo)
电话号码
x、 (电话)
@Html.ValidationMessageFor(x=>x.Phone)
@标签(“部门”、“选择部门”)
@DropDownList(“Department”,new SelectList(new[]{“IT”,“CS”}),new{@class=“form control”})
登记
当我提交表单时,它会被提交,即使没有字段被填充而不显示注释消息,或者它不会阻止提交空表单。我曾尝试在head中添加验证和无阻碍LIB,但仍然不起作用。
非常感谢您提供的任何帮助。提前感谢我相信这里的混淆是因为您同时使用了
标记和Html.textbox作为
帮助者。您不需要两者都使用,Html.TextBoxFor
方法实际上会为您生成输入标记。您可以为的textbox提供附加参数,以便使您的
标记包含类
属性(以及您想要的任何其他属性)
查看更多信息和要遵循的好例子
我相信,如果您纠正了混合标记,您会发现验证开始工作。我使用Ctrl+F5运行项目,注释不起作用,CSS也没有正确加载。我重新启动了visual studio并在调试模式下运行了该项目它工作正常,
非常感谢所有回答和帮助您的人。1)请检查您的web.config是否包含以下代码
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
2) 请检查是否在项目的页面或布局页面中添加了以下Jquery文件的引用。
jquery.validate.unobtrusive.js和jquery.validate.js很抱歉,我打算删除我刚刚忘记的标记。对于每个属性,我都使用了简单的html.textboxfor和html.validationMessagefor,但它仍然不起作用,表单即使是空字段也会被发布。您可以发布整个页面的呈现html代码吗?我发布了,但我(可能错误地)假设提供的示例代码是预期删除
标记后的结果(即不完整)。