显示RemoteAttribute的结果在MVC 3.0 [英] Display the Result of RemoteAttribute in MVC 3.0
问题描述
我有一个视图模型设置使用 RemoteAttribute
使用RemoteValidation。它工作正常。
I have a ViewModel setup to use RemoteValidation using the RemoteAttribute
. It works fine.
更新有点表现出一些固定的code。
Updated it a bit to show some fixed code.
我要指出的是,这不是我的实际注册code。这是测试,所以我可以在其他场合使用它。我没有让用户使用平板的名字注册!
I want to point out that this is not my actual "Register" code. This is testing it so I can use it in other situations. I'm not having users register using flat names!
下面是我引用库以及如何我引用他们。
Here are the libraries I am referencing, and how I am referencing them.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.js"></script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.unobtrusive-ajax.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.min.js"></script>
下面是怎么了布线RemoteAttribute。
Here is how I am wiring the 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;
}
}
然后这里是实际控制人的行为。
And then here is the actual controller behavior.
public ActionResult ValidateUserName(string name)
{
// perform logic
if (true)
return Json(true, JsonRequestBehavior.AllowGet);
return Json(false, JsonRequestBehavior.AllowGet);
}
我已经检查了我的HTML,而这个功能我想要的。但我不明白从那里做。我怎样才能显示信息给用户?它只是将其存储在HTML
I have inspected my HTML, and this functions as I want. But I don't understand what to do from there. How can I display that information to the user? It just stores it in the html
数据-VAL-远程=*是无效的
我都看过,而且我注意到,即使在 RemoteAttribute
返回false,从
I have watched, and I notice that even when the RemoteAttribute
returns false, the html changes from
值
到值类=有效
,但在我的其他模型验证,这会更改`类=输入验证错误'。
value
to value class="valid"
, but in my other model validations, this changes to `class="input-validation-error"'.
因此,没有人对如何绘制远程消息发回任何线索?或者是有真的没有什么我能做什么?
So does anyone have any clues on how to draw the remote message back? Or is there really nothing I can do?
推荐答案
以下工作正常,我:
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
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
@Html.TextBoxFor(x => x.Name)
@Html.ValidationMessageFor(x => x.Name)
<input type="submit" value="OK" />
}
您也可能会发现以下博客帖子有用的。
You may also find the following blog post useful.
这篇关于显示RemoteAttribute的结果在MVC 3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!