我在 ASP.NET MVC3 中执行远程验证,但它不起作用意味着它没有调用我在 Chrome java 脚本控制台中看到的操作。这是我的代码:模型:
public class Genre
{
public int GenreId { get; set; }
[Remote("GenreNameExist","Genre",ErrorMessage="Genre name already Exists!!!")]
public string Name { get; set; }
public String Description { get; set; }
public List<Album> Albums { get; set; }
}
控制器:
public ActionResult GenreNameExist(Genre genre)
{
var gen = db.Genres.Where(x=>x.Name==genre.Name).FirstOrDefault();
if (gen == null)
{
return Json(true, JsonRequestBehavior.AllowGet);
}
else {
return Json(string.Format("{0} is not available.", Name), JsonRequestBehavior.AllowGet);
}
}
和查看:
@Html.ValidationSummary(true);
<div class="editor-label">
@Html.LabelFor(x => x.Name)
</div>
<div class="editor-field">
@Html.TextBoxFor(x => x.Name)
@Html.ValidationMessageFor(x=>x.Name)
</div>
提前致谢!!!