我想在我的MVC項目(單聲道/ .NET 4.5)中使用數據註釋。我創建了我的模型並添加了所有適當的註釋。我有我的看法和控制器適當連線。然而,驗證似乎並沒有發生。我嘗試了一切,我可以找到無濟於事。由於這是我第一次使用Razor和Data Annotations,所以我想象一下我錯過了一些安裝程序,但是在我的生活中找不到它。這裏是我的代碼:在剃刀MVC數據註釋
型號
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace MyWebsite
{
public class RegisterViewModel : BaseViewModel
{
#region properties
[Required(ErrorMessage = "Required")]
[StringLength(50)]
[DisplayName("First Name")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Required")]
[StringLength(100)]
[DisplayName("Last Name")]
public string LastName { get; set; }
[StringLength(50)]
[DisplayName("Display Name")]
public string DisplayName { get; set; }
[Required(ErrorMessage = "Required")]
[StringLength(255)]
[DisplayName("Email Address")]
public string EmailAddress { get; set; }
#endregion
#region ctor
public RegisterViewModel()
{
}
#endregion
}
}
控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MyWebsite.Controllers
{
public class AccountController : Controller
{
public ActionResult Register()
{
ViewData ["IsComplete"] = false;
ViewData ["RequiredVouches"] = WebSettings.RequiredVouches;
return View();
}
[HttpPost]
public ActionResult Register(FormCollection formData)
{
if (ModelState.IsValid) {
//TODO: Put the data
ViewData ["IsComplete"] = true;
}
return View();
}
}
}
查看
@model SummerIsles.Web.RegisterViewModel
@section Styles {
<link href="~/Content/themes/base/Account.css" rel="stylesheet" type="text/css" />
}
@{
if((bool)@ViewData["IsComplete"]) {
<h1>Registration Request Complete!</h1>
<div class="page-message">
<p>
Confirmation message goes here
</p>
</div>
}
else {
<h1>Registration</h1>
<div class="page-message">
<p>
Instruction text goes here
</p>
</div>
using (Html.BeginForm()) {
@Html.ValidationSummary(false)
<fieldset>
<legend>Your Information</legend>
<div class="group column-1">
@Html.LabelFor(modelItem => @Model.FirstName)
@Html.EditorFor(modelItem => @Model.FirstName, new { htmlAttributes = new { @class="form-control" } })
@Html.ValidationMessageFor(modelItem => @Model.FirstName)
@Html.LabelFor(modelItem => @Model.DisplayName)
@Html.EditorFor(modelItem => @Model.DisplayName, new { htmlAttributes = new { @class="form-control" } })
@Html.ValidationMessageFor(modelItem => @Model.DisplayName)
</div>
<div class="group column-2">
@Html.LabelFor(modelItem => @Model.LastName)
@Html.EditorFor(modelItem => @Model.LastName, new { htmlAttributes = new { @class="form-control" } })
@Html.ValidationMessageFor(modelItem => @Model.LastName)
@Html.LabelFor(modelItem => @Model.EmailAddress)
@Html.EditorFor(modelItem => @Model.EmailAddress, new { htmlAttributes = new { @class="form-control" } })
@Html.ValidationMessageFor(modelItem => @Model.EmailAddress)
</div>
<div class="button-options">
<button id="btnSubmit" type="submit" formmethod="post" class="btn btn-danger">Submit</button>
<a id="btnCancel" href="~/" class="btn">Cancel</a>
</div>
</fieldset>
}
}
}
另外,我公頃我已經將jquery驗證腳本添加到了我的佈局文件中,並且還啓用了web.confg中的客戶端驗證。
佈局頁眉
<head>
<!--some other css and such-->
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobstrusive.min.js"></script>
</head>
的Web.config
<appSettings>
<!--some other stuff-->
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
基本上,當我點擊提交,它認爲該模型是完全有效的(ModelState.IsValid控制器返回true )和驗證的東西(我希望在發回控制器之前發射)似乎決不會觸發。即使使用完全空白的表單(儘管有「必需的」數據註釋),我也不會收到驗證消息。我錯過了什麼?
在服務器上,「ModelState」永遠不會失效,因爲您不使用模型。把你的POST方法改爲'public ActionResult Register(RegisterViewModel model)'在客戶端你還包含了'jquery- {version} .js'? –
你是否在使用你的腳本包? –