public static GradingJSON ToModel(this grades_method row) { return(new GradingJSON() { id = row.id, name = row.name }); }
public ActionResult Save(GradingJSON method) { var single = new grades_method(); if (method.id.HasValue) { single = db.grades_methods.Single(x => x.id == method.id.Value); // delete existing var postedIDs = method.rules.Where(y => y.id.HasValue).Select(y => y.id.Value).ToArray(); var todelete = single.grades_rules.Where(x => !postedIDs.Contains(x.id)); db.grades_rules.DeleteAllOnSubmit(todelete); // update existing foreach (var entry in method.rules.Where(x => x.id.HasValue)) { var rule = single.grades_rules.Single(x => x.id == entry.id.Value); rule.grade = entry.grade; rule.gradepoint = entry.gradepoint; if (!entry.mark.HasValue) { return(Json("A mark must be specified for each rule added".ToJsonFail())); } rule.mark = entry.mark.Value; } } else { db.grades_methods.InsertOnSubmit(single); } single.name = method.name; // insert new foreach (var entry in method.rules.Where(x => !x.id.HasValue)) { var rule = new grades_rule(); rule.grade = entry.grade; rule.gradepoint = entry.gradepoint; if (!entry.mark.HasValue) { return(Json("A mark must be specified for each rule added".ToJsonFail())); } rule.mark = entry.mark.Value; single.grades_rules.Add(rule); } repository.Save(); var viewmodel = "Entry saved successfully".ToJsonOKMessage(); viewmodel.data = this.RenderViewToString("Rows", new[] { single.ToModel() }); return(Json(viewmodel)); }