我还是ASP.NET MVC的新手,我正在努力使用我需要实现的验证器。
我的UserName
模型中有Account
属性,需要检查其唯一性。
它被声明如下:
[Required(ErrorMessageResourceName = "UserNameRequiredError", ErrorMessageResourceType = typeof(App_LocalResources.Resources))]
[Display(Name = "UserName", ResourceType = typeof(App_LocalResources.Resources))]
[VallidateAccount(Field = AccountChecks.UserNameUnique, ErrorMessageResourceName = "UserNameUniqueError", ErrorMessageResourceType = typeof(App_LocalResources.Resources))]
public string UserName { get; set; }
使用过的ValidationAttribute执行以下检查
public class VallidateAccountAttribute : ValidationAttribute
{
public AccountChecks Field { get; set; }
public int ID { get; set; }
public override bool IsValid(object value)
{
switch (Field){
case AccountChecks.UserNameUnique:
return CheckUserName((string)value);
//Other Fields
default:
throw new ParameterException("Invallid Field");
}
}
private bool CheckUserName(string username)
{
Account account = Connection.DB.Accounts.FirstOrDefault(x => x.UserName == username);
return !string.IsNullOrEmpty(username) && (account == null || account.AccountID == this.ID);
}
//Other checks
}
现在针对这个问题: 输入的用户名通过IsValid方法的value参数正确传递给验证,但我找不到传递ID的方法。
我尝试按照与传递Field参数相同的方式传递它,但我无法使用this
关键字。
[VallidateAccount(Field = AccountChecks.UserNameUnique, ID = this.AccountID, ErrorMessageResourceName = "UserNameUniqueError", ErrorMessageResourceType = typeof(App_LocalResources.Resources))]
任何人都可以帮助我通过ID
值吗?
远程验证使我能够完全按照自己的意愿行事。
新的UserName
属性:
[Required(ErrorMessageResourceName = "UserNameRequiredError", ErrorMessageResourceType = typeof(App_LocalResources.Resources))]
[Display(Name = "UserName", ResourceType = typeof(App_LocalResources.Resources))]
[System.Web.Mvc.Remote("UserNameUnique", "Validation", AdditionalFields = "AccountID", HttpMethod = "POST", ErrorMessageResourceName = "UserNameUniqueError", ErrorMessageResourceType = typeof(App_LocalResources.Resources))]
public string UserName { get; set; }
在这种情况下不再使用ValidationAttribute
,而是将其替换为新的ValidationController
。
using System.Web.Mvc;
using MyProject.Models;
namespace MyProject.Controllers
{
[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
public class ValidationController : Controller
{
MyDbContext _repository;
public ValidationController() : this(Connection.DB) { }
public ValidationController(MyDbContext repository)
{
_repository = repository;
}
//NOTE: Make sure the parameters have the same name as your properties!!!
public JsonResult UserNameUnique(string UserName, int AccountID = 0)
{
Account account = Connection.DB.Accounts.FirstOrDefault(x => x.UserName == UserName);
return Json(!string.IsNullOrEmpty(UserName) && (account == null || account.AccountID == AccountID), JsonRequestBehavior.AllowGet);
}
}
}
答案 0 :(得分:1)
您可以在此方案中执行远程端验证。 通过它,您可以轻松检查Exsisting UserName。
将此http://msdn.microsoft.com/en-us/library/gg508808(v=vs.98).aspx用于参考
答案 1 :(得分:0)
你做得很多!如果您想验证唯一性,那就这样做吧。
如果你有.FirstOrDefault,你应该改为.Any,然后检查是否有任何包含该用户名的行。
private bool CheckUserName(string username){ 如果(string.IsNullOrEmpty(用户名)) 返回false;
return!Connection.DB.Accounts.Any(x => x.UserName == username); }