Question

I'm having some problems with Model.IsValid. I am trying to create a model similar to this...

class Article {
  int Id {get; set;}
  string Title {get; set;}

  Category Category {get; set;}
}

class Category {
  int Id {get; set;}
  string Name {get; set;}

  IList<Articles> Articles {get; set;}
}

I would like that the Article Category can be null. However Model.IsValid is always false when the Category is null.

Any help appreciated.

Was it helpful?

Solution

Hmmm ... are you sure the Model.IsValid is false because of category being null? I am asking this since I don't see any 'required' data annotations in the Category property in your domain object. OR if it is indeed failing when binding on the Category then you might need to create a custom model binder which is where I think - more likely a guess - your error is happening.

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