public static ExamTemplate ToEditModel(this exam_template row) { return(new ExamTemplate() { id = row.id.ToString(), name = row.name, maxmark = row.maxMark.ToString(), schoolid = row.schoolid, description = row.description, subjects = row.exam_template_subjects.OrderBy(x => x.position).ToModel(), isPrivate = row.isprivate }); }
public ActionResult Save(int?id, string description, bool isprivate, string template_name, long?[] subject, string[] subjectname, string[] code, int school, short maxmark, int?[] templatesubjectid) { var template = new exam_template(); if (id.HasValue) { template = repository.GetExamTemplate(id.Value); // only allow creator to update if (template.creator.HasValue && template.creator.Value != sessionid.Value) { return(Json("Only the template creator can edit this".ToJsonFail())); } } else { // put things you don't want changed here when editing a template template.creator = sessionid.Value; } template.schoolid = school; template.maxMark = maxmark; template.name = template_name; template.description = description; template.isprivate = isprivate; if (subjectname != null) { for (int i = 0; i < subjectname.Length; i++) { var sname = subjectname[i]; var c = code[i]; var s = subject[i]; var existingid = templatesubjectid[i]; if (string.IsNullOrEmpty(sname)) { // ignore blank rows continue; } var entry = new exam_template_subject(); if (existingid.HasValue) { entry = template.exam_template_subjects.SingleOrDefault(x => x.id == existingid); if (entry == null) { entry = new exam_template_subject(); } } entry.code = c; entry.subjectid = s; entry.name = sname; if (!existingid.HasValue) { template.exam_template_subjects.Add(entry); } } } if (!id.HasValue) { repository.AddExamTemplate(template); } try { repository.Save(); } catch (Exception ex) { return(SendJsonErrorResponse(ex)); } var viewmodel = template.id.ToJsonOKData(); viewmodel.message = "Template saved successfully"; return(Json(viewmodel)); }
public void AddExamTemplate(exam_template template) { db.exam_templates.InsertOnSubmit(template); }