0

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

adiga
  • 34,372
  • 9
  • 61
  • 83
Learner
  • 1,277
  • 3
  • 15
  • 34
  • It looks like you are asking the wrong question. What do u want to achieve? – Ibrahim ben Salah Nov 23 '14 at 17:23
  • If you don't post back values for the dates then the values wont be added to `ModelState`, and in any case you should use view models to represent what you want to display/edit. What exactly are you trying to do? –  Nov 23 '14 at 21:42
  • @IbrahimbenSalah when updating a student record the CreatedOn should not be updated. so ModelState.IsValid is always false in my case. but if i put this in hiddenfield and pass to my model it works. this is just for learning how can i exclude/Clear something from ModelState.if it is specified in collection. – Learner Nov 24 '14 at 04:10
  • @StephenMuecke , Hi Stephen. actually i want to know how to exclude/Clear a value of field from ModelState. If it is in collection. e.g. person object has collection of student and update method on person controller should exclude/Clear ModelState for student collection. I am sorry if i am unable to explain. – Learner Nov 24 '14 at 04:14
  • @iGod, For all student properties of just the `CreatedOn` and/or `UpdatedOn` properties? –  Nov 24 '14 at 04:18
  • 1
    get some idea from http://stackoverflow.com/questions/24130515/remove-object-from-modelstate-validation/24135311#24135311 – RollerCosta Mar 16 '15 at 07:35
  • Thanks RollerCosta. You made this quite simple. Really Man you are Genius – Learner Mar 17 '15 at 12:02

2 Answers2

1

I had answered similar question before here

For those who can't afford one extra click to visit the link

  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

Option 1 and 2 will work only when client side validation is disabled.

RollerCosta
  • 5,020
  • 9
  • 53
  • 71
0

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.