我有一個簡單的模型是默認RegisterModel
並在創建Create
基於該模型視圖我最終ASP.NET MVC3遠程驗證不輸出錯誤信息
public class RegisterModel
{
[Required]
[Remote("UserNameExists", "Account", "", ErrorMessage = "Username is already taken.")]
[Display(Name = "Username (spaces will be stripped, must be at least 6 characters long)")]
public string UserName { get; set; }
[Required]
[Editable(true)]
[Display(Name = "First and Last name")]
public string Name { get; set; }
[Required]
[DataType(DataType.EmailAddress, ErrorMessage = "You need to enter a valid email")]
[Remote("EmailExists", "Account", "", ErrorMessage = "Email is already taken.")]
[Display(Name = "Email address")]
public string Email { get; set; }
//[Required]
//[ValidatePasswordLength]
[DataType(DataType.Password)]
[Display(Name = "Create a password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Verify password")]
//[Compare("Password", ErrorMessage = "Password's do not match.")]
public string ConfirmPassword { get; set; }
}
,並在觀:
<h3>
Details</h3>
@using (Html.BeginForm("GenerateBetaLink", "Account", FormMethod.Post, new { @id = "beta-user" }))
{
@Html.ValidationSummary(true)
<div>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<p>
</p>
<p>
<input type="submit" value="Create Beta User" class="btn-submit" />
<span class="loading"></span>
</p>
</div>
}
我Validation Controller
public class ValidationController : Controller
{
public JsonResult UserNameExists(string UserName)
{
OnlineServicesRepository db = new OnlineServicesRepository();
var user = db.FindUserByUsername(UserName.Trim());
return user == null ?
Json(true, JsonRequestBehavior.AllowGet) :
Json(string.Format("{0} is not available.", UserName),
JsonRequestBehavior.AllowGet);
}
public JsonResult EmailExists(string Email)
{
OnlineServicesRepository db = new OnlineServicesRepository();
var user = db.FindUserByEmail(Email.Trim());
return user != null ?
Json(true, JsonRequestBehavior.AllowGet) :
Json(string.Format("{0} is not available.", Email),
JsonRequestBehavior.AllowGet);
}
}
我的問題是是遠程驗證不火,但不寫任何東西到錯誤消息,因爲它應該加上,jQuery的方法.valid()
不斷告訴我該形式是有效:
http://www.balexandre.com/temp/2011-04-19_0900.png
我缺少什麼在這裏嗎?
的MSDN article顯示了相同的代碼(在可下載的文件)
Dang,我根本找不到我的工作:(我的'ValidationMessageFor'在那裏,但它從來沒有被填充......沒有任何javascript錯誤,我可以在控制檯中看到,它只是...沒有想法(是的,如果你想知道,我的視圖中有validate.js和unobtrusive.js文件):/ – balexandre 2011-04-19 07:42:09
@balexandre,你的控制器動作是怎樣的? – 2011-04-19 07:53:15
剛剛添加到問題中,但工作正常,傳遞正確的值,正如你可以在FireBug控制檯中看到的一樣......我只是使用電子郵件創建了一個簡單的視圖,但我仍然沒有得到驗證消息:( – balexandre 2011-04-19 08:03:28