MVC3远程属性 - 验证 [英] MVC3 Remote Attribute - validation
本文介绍了MVC3远程属性 - 验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类管理:
public class Admin
{
public virtual int AdminId { get; set; }
[Remote("UsernameAvailable", "Admins")]
[Display(Name = "lblUsername", ResourceType = typeof(Resources.Admin.Controllers.Admins))]
public virtual string Username { get; set; }
...
然后我有一个是用于视图视图模型类:
then i have a viewmodel class that's used for a view:
public class AdminsEditViewModel
{
public Admin Admin { get; set; }
public IEnumerable<SelectListItem> SelectAdminsInGroup { get; set; }
...
控制器:
public ActionResult UsernameAvailable(string Username)
{
return Json(this.AdminRepository.GetLoginByUsername(Username), JsonRequestBehavior.AllowGet);
}
不过用户名字符串总是空,因为什么被发送到动作是这样的:
However string Username is always null because what is sent to Action is this:
http://localhost/admin/admins/usernameavailable?Admin.Username=ferwf
问题是,UsernameAvailable发送Admin.Username值和HTTP查询不名价值。我会怎么做使用视图模型呢?
The problem is that UsernameAvailable sends Admin.Username value and NOT Username value in the http query. how would I do it using a view model?
感谢
推荐答案
您可以指定一个preFIX到默认的模型绑定:
You could specify a prefix to the default model binder:
public ActionResult UsernameAvailable([Bind(Prefix = "Admin")]string username)
{
return Json(
this.AdminRepository.GetLoginByUsername(username),
JsonRequestBehavior.AllowGet
);
}
或使用管理
模型:
public ActionResult UsernameAvailable(Admin admin)
{
return Json(
this.AdminRepository.GetLoginByUsername(admin.Username),
JsonRequestBehavior.AllowGet
);
}
现在用户名
参数将被正确绑定假设有如下要求:
Now username
parameter will be correctly bound assuming the following request:
http://localhost/admin/admins/usernameavailable?Admin.Username=ferwf
这篇关于MVC3远程属性 - 验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文