gpt4 book ai didi

asp.net-mvc - MVC3 远程属性 - 验证

转载 作者:行者123 更新时间:2023-12-01 11:58:07 25 4
gpt4 key购买 nike

我有一个类(class)管理员:

    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; }
...

然后我有一个用于 View 的 View 模型类:

   public class AdminsEditViewModel 
{

public Admin Admin { get; set; }

public IEnumerable<SelectListItem> SelectAdminsInGroup { get; set; }
...

Controller :

public ActionResult UsernameAvailable(string Username)
{
return Json(this.AdminRepository.GetLoginByUsername(Username), JsonRequestBehavior.AllowGet);

}

但是字符串 Username 始终为 null,因为发送给 Action 的是这样的:

http://localhost/admin/admins/usernameavailable?Admin.Username=ferwf

问题是 UsernameAvailable 在 http 查询中发送 Admin.Username 值而不是 Username 值。我将如何使用 View 模型来做到这一点?

谢谢

最佳答案

您可以为默认模型绑定(bind)程序指定一个前缀:

public ActionResult UsernameAvailable([Bind(Prefix = "Admin")]string username)
{
return Json(
this.AdminRepository.GetLoginByUsername(username),
JsonRequestBehavior.AllowGet
);
}

或使用您的Admin 模型:

public ActionResult UsernameAvailable(Admin admin)
{
return Json(
this.AdminRepository.GetLoginByUsername(admin.Username),
JsonRequestBehavior.AllowGet
);
}

现在 username 参数将被正确绑定(bind),假设有以下请求:

http://localhost/admin/admins/usernameavailable?Admin.Username=ferwf

关于asp.net-mvc - MVC3 远程属性 - 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4833725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com