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);
}