Question

I am trying to use ModelState.IsValid which at the moment is always comeing back false because one field in the model is empty in an object.

What i want to do is remove this object within this method and then try to ModelState.IsValid on the altered model?

loyaltyOffers = model.Offers.Where(m => m.Reward == "").ToList();
foreach (Offer offer in Offers)
{
   model.Offers.Remove(offer);
}       
if (ModelState.IsValid)
{
Was it helpful?

Solution

When you are done altering the model, use this:

if (TryValidateModel(model))
    // Valid!!!

MSDN

OTHER TIPS

Alternatively, you can just remove the error from the ModelState:

ModelState.Remove("key");

Where "key" is the key for the error in the invalid model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top