我正在尝试实现数据注释。我创建了一个名为 RegisterViewModel 的视图模型,其中包含以下数据注释。
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>
当我提交表单时,即使没有填写字段而没有显示注释消息,它也会被提交,或者它不会阻止提交空表单。我已经尝试在头部添加验证和无阻碍的库,但它仍然没有工作。 任何帮助都非常感谢。谢谢提前
答案 0 :(得分:0)
我认为这里的混淆是因为您正在使用<input>
标记以及Html.TextBoxFor
帮助程序。您不需要两者,Html.TextBoxFor
方法实际上会为您生成输入标记。您可以向TextBoxFor
提供其他参数,以使<input>
代码包含class
属性(以及您希望的任何其他属性)。
查看this tutorial以获取更多信息和良好示例。
我相信,如果你更正混合标签,你会发现验证开始有效。
答案 1 :(得分:0)
我用Ctrl + F5运行项目,注释不起作用,也没有正确加载CSS。我重新启动了visual studio并在调试模式下运行了它正常工作的项目, 非常感谢所有有回答和帮助的人。
答案 2 :(得分:0)
1)请检查您的网站.config是否包含以下代码。
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
2)请检查您是否在项目的页面或布局页面中添加了以下Jquery文件的引用。 jquery.validate.unobtrusive.js和jquery.validate.js