远程归属validation不在Asp.Net MVC中触发

这是我的型号代码

public class BlobAppModel { [Required(ErrorMessage="Please enter the name of the image")] [Remote("IsNameAvailable","Home",ErrorMessage="Name Already Exists")] public string Name { set; get; } } 

在我的控制器中我有

 public JsonResult IsNameAvailable(string Name) { bool xx= BlobManager.IsNameAvailable(Name); if (!xx) { return Json("The name already exists", JsonRequestBehavior.AllowGet); } return Json(true, JsonRequestBehavior.AllowGet); } 

在我的数据中我有

 public static bool IsNameAvailable(string Name) { var test = ""; using (var x = new BlobTestAppDBEntities()) { try { test=x.BlobApps.Where(m => m.Name == Name).FirstOrDefault().Uri; if (test != null) return false; else return true; } catch (Exception) { return true; } } } 

在我看来,我也添加了脚本

   @using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) {  @Html.Label("Name:") @Html.TextBoxFor(m => m.Name) @Html.ValidationMessageFor(m=>m.Name)} 

但远程validation根本没有解决。我的代码有什么问题吗?

确保您的validation方法使用[AllowAnonymous]属性进行修饰(也可能需要[HttpPost])。

 [AllowAnonymous] public JsonResult IsNameAvailable(string Name) 

还有一个提示:使用浏览器的开发人员工具(主流浏览器中的F12按钮)来查看Json请求的状态。

您在视图中缺少jquery.unobtrusive-ajax.js文件,请添加并再次尝试。

  

如果你没有它从nuget进入

nuget link http://www.nuget.org/packages/Microsoft.jQuery.Unobtrusive.Ajax/

我知道这有点晚了,如果你使用的是Razor语法,我发现除了你所做的一切之外,你还需要:

 @Scripts.Render("~/bundles/jqueryval") 

这些设置也需要进入web.config: