Reputation: 1315
I have a class Person. Which is associated with the Students. now students class contain a datefield "CreatedOn".
class Person
{
String Name;
List<Student> students;
-----rest of props and methods
}
class Student
{
DateTime CreatedOn;
DateTime UpdatedOn;
---------rest of props and methods
}
now my problem is I want to remove these dates (of student class) from modelstate while updating object of person class. How can I do that?
since a person can have 2 student and other can have any number of students.
if I need to remove "Name" from ModelState I would have used ModelState.Remove("Name") for person but how can I do it for students of person. Is there something like ModelState.Remove("students") or ModelState.Remove("students[0]") etc?
Please help me with example
Upvotes: 0
Views: 1836
Reputation: 5196
I had answered similar question before here
For those who can't afford one extra click to visit the link
Option 1 and 2 will work only when client side validation is disabled.
Upvotes: 1
Reputation: 733
Add two viewmodels one for update action and one for create action and exclude createdon and updatedon properties from the viewmodels, This way you keep your controllers clean from additional complexity of clearing or adding errors and just test for ModelState.IsValid. If valid then update or create the Student with updatedon or createdon respectivily.
Upvotes: 0