在MVC 3.0中显示RemoteAttribute的结果

我有一个ViewModel设置,使用RemoteAttribute来使用RemoteValidation。 它工作正常。

编辑

更新了一下以显示一些固定代码。

我想指出这不是我的实际“注册”代码。 这是测试它,所以我可以在其他情况下使用它。 我没有用户注册使用平面名称!

这是我引用的库,以及我如何引用它们。

      

这是我如何连接RemoteAttribute。

 public class UserRegistrationModel { [Required] [RegularExpression(@"^(?:[a-zA-Z\p{L} \.'\-]{3,48})$", ErrorMessage = "This name contains invalid characters. Names must be between 3 and 48 characters, contain only standard unicode symbols, and may not contain any punctuation other than the ['] and [-] symbols.")] [Remote("ValidateUserName", "Membership", ErrorMessage = "{0} is invalid.")] public string Name { get; set; } } 

然后这是实际的控制器行为。

  public ActionResult ValidateUserName(string name) { // perform logic if (true) return Json(true, JsonRequestBehavior.AllowGet); return Json(false, JsonRequestBehavior.AllowGet); } 

我检查过我的HTML,这个function正如我想要的那样。 但我不明白从那里做什么。 如何向用户显示该信息? 它只是将它存储在html中

data-val-remote="* is invalid"

我已经看过了,我注意到即使RemoteAttribute返回false,html也会改变

value to value class="valid" ,但在我的其他模型validation中,这会改为`class =“input-validation-error”’。

那么有没有人有关于如何绘制远程消息的任何线索? 或者我真的无能为力吗?

以下工作对我来说很好:

 public class UserRegistrationViewModel { [Required] [RegularExpression(@"^(?:[a-zA-Z\p{L} \.'\-]{3,48})$", ErrorMessage = "This name contains invalid characters. Names must be between 3 and 48 characters, contain only standard unicode symbols, and may not contain any punctuation other than the ['] and [-] symbols.")] [Remote("ValidateUniqueName", "Home")] public string Name { get; set; } } 

控制器:

 public class HomeController : Controller { public ActionResult Index() { return View(new UserRegistrationViewModel()); } public ActionResult ValidateUniqueName(string Name) { if (NameIsValid(Name)) { return Json(true, JsonRequestBehavior.AllowGet); } return Json(string.Format("{0} is invalid", Name), JsonRequestBehavior.AllowGet); } } 

视图:

 @model AppName.Models.UserRegistrationViewModel   @using (Html.BeginForm()) { @Html.TextBoxFor(x => x.Name) @Html.ValidationMessageFor(x => x.Name)  } 

您可能还会发现以下博文有用。