ModelState.IsValid VS IValidateableObject在MVC3 [英] ModelState.IsValid vs IValidateableObject in MVC3

查看:152
本文介绍了ModelState.IsValid VS IValidateableObject在MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以要根据<一个href=\"http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-$p$pview-1.aspx\">Gu IValidatableObject.Validate()当控制器验证它的模式应该被调用(即前 ModelState.IsValid ),但是简单地使该模型实施 IValidatableObject 似乎并没有工作,因为验证(..)不会被调用。

so according to Gu IValidatableObject.Validate() should get called when a controller validates it's model (i.e. before ModelState.IsValid) however simply making the model implement IValidatableObject doesn't seem to work, because Validate(..) doesn't get called.

任何人都知道,如果有别的东西,我必须要连接得到这个工作?

Anyone know if there is something else I have to wire up to get this to work?

编辑:

下面是要求code。

public class LoginModel : IValidatableObject
{
    [Required]
    [Description("Email Address")]
    public string Email { get; set; }

    [Required]
    [Description("Password")]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [DisplayName("Remember Me")]
    public bool RememberMe { get; set; }

    public int UserPk { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        var result = DataContext.Fetch( db => {

            var user = db.Users.FirstOrDefault(u => u.Email == Email);

            if (user == null) return new ValidationResult("That email address doesn't exist."); 
            if (user.Password != User.CreateHash(Password, user.Salt)) return new ValidationResult("The password supplied is incorrect.");

            UserPk = user.UserPk;
            return null;
        });

        return new List<ValidationResult>(){ result };
    }
}

的操作。 (我没有做任何事情在控制器特殊...)

The action. ( I don't do anything special in the Controller...)

[HttpPost]
public ActionResult Login(LoginModel model)
{
    if (ModelState.IsValid)
    {
        FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe);
        return Redirect(Request.UrlReferrer.AbsolutePath);
    }

    if (ControllerContext.IsChildAction || Request.IsAjaxRequest())
        return View("LoginForm", model);

    return View(model);
}

我设置 LoginModel.Validate()的第一行一个破发点,它似乎并没有被击中。​​

I set a break point on the first line of LoginModel.Validate() and it doesn't seem to get hit.

推荐答案

有没有什么比你只需将它添加到你验证模型的更多。下面是验证的例子

There isn't anything more than that you just have to add it to the model you're validating. Here's an example of validation

public class User : IValidatableObject {
    public Int32 UserID { get; set; }
    public string Name { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
        //do your validation

        return new List<ValidationResult>();
    }
}

和您的控制器将使用该模型

And your controller would use this model

public ActionResult Edit(User user) {
    if (ModelState.IsValid) {
    }
}

希望这有助于。其他要求.NET 4和数据注解 - 你显然需要的仅仅指刚ivalidatableobject。张贴任何问题,我们会看到,如果我们不能解决的话 - 就像发表您的模型和控制器......你可能会失去了一些东西。

Hope this helps. Other requirements are .net 4 and data annotations - which you obviously need jsut for ivalidatableobject. Post any issues and we'll see if we can't resolve them - like post your model and your controller...you might be missing something.

这篇关于ModelState.IsValid VS IValidateableObject在MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆