2

I have two models:

public class UserInfo
{
    public long ID { get; set; }

    [Required]
    [StringLength(50)]
    public string FirstName { get; set; }

    //...

    public bool Falg{ get; set; }
}

public class UserInfoExtra
{
    public long ID { get; set; }

    [Required]
    public string PhoneNumber { get; set; }

    //...
}

Those two models are combined in a ViewModel:

public class UserViewModel
{
    public UserInfo UserInfo { get; set; }
    public UserInfoExtra ExtraInfo { get; set; }
}

In the controller:

public ActionResult Create(UserViewModel userinfo)
{
    if(userInfo.Flag){
        //Remove ExtaInfo from validation in ModelState.IsValid?
    }
    if (ModelState.IsValid)
    {
        db.UserInfos.Add(userinfo);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(userinfo);
}

How can I remove the ExtraInfo from validation in ModelState.IsValid?

ataravati
  • 8,891
  • 9
  • 57
  • 89
amp
  • 11,754
  • 18
  • 77
  • 133
  • 1
    You can use `ModelState[key].Errors.Clear();` to remove individual errors –  Jun 09 '14 at 23:29
  • You can go with Stephen's comment or if you have client validation enabled then do force submit(form) with jquery and on server side just do ModelState[key].Errors.Clear(); or if(ModelState.IsValidField(UserInfo)){//model state for userinfor is valid } – RollerCosta Jun 10 '14 at 07:22

4 Answers4

3

Removing errors from ModelState is not a good idea. If you don't want the UserInfoExtra class to be validated when using the UserViewModel, then you should define your View Model differently (like below), and then use conditional validation on the PhoneNumber property:

public class UserViewModel
{
    public UserInfo UserInfo { get; set; }
    public long ID { get; set; }
    //[RequiredIf] (conditional validation here)...
    public string PhoneNumber { get; set;}
}

Here you can find an implementation of RequiredIf attribute: RequiredIf Conditional Validation Attribute

Community
  • 1
  • 1
ataravati
  • 8,891
  • 9
  • 57
  • 89
1
  1. Ignore other properties(other than UserInfo) : ModelState.IsValidField(UserInfo)
  2. Clear/Remove property error : ModelState["ExtraInfo"].Errors.Clear();
  3. Create custom validator, as also suggested by ataravati : MVC Custom validation attribute
RollerCosta
  • 5,020
  • 9
  • 53
  • 71
0

Here is an extension method that you can write to group all the modelstate errors as a Dictionary collection and now you can remove an item using it's key.

public static IEnumerable Errors(this ModelStateDictionary modelState)
{
    if (!modelState.IsValid)
    {
        return modelState.ToDictionary(kvp => kvp.Key,
            kvp => kvp.Value.Errors
                            .Select(e => e.ErrorMessage).ToArray())
                            .Where(m => m.Value.Count() > 0);
    }
    return null;
}

and you can access the modelstate error collection by var errors = ModelState.Errors();

ataravati
  • 8,891
  • 9
  • 57
  • 89
Dennis R
  • 3,195
  • 1
  • 19
  • 24
0

You can try :

public ActionResult Create([Bind(Exclude="ExtraInfo")]UserViewModel userinfo)
{
...
}

Hope will help.

Anupam Singh
  • 1,158
  • 13
  • 25