我有这个 ViewModel
public class CustomerSuscribeViewModel : IValidatableObject
{
[DataMember(IsRequired = true)]
[DataType(DataType.Text)]
public string Name { get; set; }
public string SurName { get; set; }
[DataMember(IsRequired = true)]
[DataType(DataType.Password)]
public string Password { get; set; }
[DataMember(IsRequired = true)]
[DataType(DataType.Password)]
public string ConfirmPassword { get; set; }
[DataMember(IsRequired = true)]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Inject]
public IDataUsers DataUsers { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if(DataUsers.Get(new UserFilter() {Email = Email}).Any())
{
yield return new ValidationResult(This Email is already used.",new string[]{"Email"});
}
if(ConfirmPassword != Password)
yield return new ValidationResult("Please confirm your apssword", new string[] { "Password", "ConfirmPassword" });
}
}
而这个动作
[HttpPost]
public ActionResult Suscribe(CustomerSuscribeViewModel customer)
{
if (!ModelState.IsValid)
return View(customer);
问题是永远不会创建成员 DataUsers,所以我得到了 NullReferenceException。
绑定通过以下方式完成:
Bind<IDataUsers>().To<DataUsers>();
任何的想法 ?
编辑: InjectAttribute 适用于我的控制器内的属性。
环境:asp.net mvc 3,ninject 2.2(也许我需要更新?)