1

I have a viewmodel that I would like to skip some parts of the details that i'm saving based on a few condition and I can't find a way to do so without repeating code (that I know is a bad practice) What I need to do is to add items to model subject (SubjectId) (good||bad ||ugly) +1 according to user choose so at the end I will have :subject name with (X good,X bad,X ugly) I think the code will help me explain myself better

 //subject table is one-to-many i need to add only the "grade" in case the subject name already exists 
 if (model.rev.GBU == "Good")
 {
      model.sub.Good = +1;
 }
 else if (model.rev.GBU == "Bad")
 {
      model.sub.bad = +1;
 }
 else if (model.rev.GBU == "Ugly")
 {
      model.sub.Ugly = +1;
 }

 //checked if the subject name is already in the db
 var subjectNameQuery = db.Subject.Where(n => n.SubjectName.ToLower().Equals(model.sub.SubjectName));
 if (subjectNameQuery.Count() > 0)
 {
      //if the name is in the table, do Not add it again
      model.rev.SubjectId = subjectNameQuery.SingleOrDefault().SubjectId;
 }
 else
 {
      db.Subject.Add(model.sub);
 }
Danny
  • 301
  • 1
  • 4
  • 21
  • When you say "skip same parts of the details" what exactly do you mean? Are you trying to update `model.rev` to the database and not `model.sub`? I'm not entirely sure what you're trying to ask here, sorry. – Rhys Feb 03 '16 at 13:13
  • I have update my Q, I hope it's clearer now. – Danny Feb 03 '16 at 13:25
  • Your question is slightly clearer now, thanks. I'm not entirely sure why you're setting the `SubjectId` variable from the `model.rev` object to `subjectNameQuery.SingleOrDefault().SubjectId;`? Look at http://stackoverflow.com/questions/15336248/entity-framework-5-updating-a-record - this might help you when trying to update a particular record. – Rhys Feb 03 '16 at 13:39

0 Answers0