How to persist edits in any column of a joined table.

How to persist edits in any column of a joined table.

rdmrdm Posts: 194Questions: 55Answers: 4
edited September 2017 in Editor

Using this page as inspiration, I set up a joined table in my controller.

I have discovered that while I can edit some columns, any edits I make on the "joined" table are not applied. Is it possible to apply edits to joined columns? Below you see two sections of code in my controller: (1) the query that populates a list that is applied to the "Teacher" field, and (2) the new Editor definition, in which the Teacher list is applied to the "Teacher" field.

I tried to keep the pasted to code to a minimum, but I would be happy to also paste any relevant jQuery code in case there might be a clue there.

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
        public ActionResult JoinedTableTest(string campus = "CRA")
        {
            var settings = Properties.Settings.Default;
            var formData = HttpContext.Request.Form;
            using (var db = new Database(settings.DbType, settings.DbConnection))
            {
                // Teacher dropdown to allow change of teacher on inline editing
                // ok for now, but would like something more secure, like a paramaterized query
                var sql = $"select distinct t.Teacher from dbo.TeacherCourseTest t where t.Campus = '{campus}' and not(t.Teacher = '' or t.Teacher is null) order by t.Teacher";
                var l = db.Sql(sql).DataTable();
                var teacherList = (from DataRow r in l.Rows
                    select r["Teacher"].ToString()
                    into t
                    select new Dictionary<string, object>
                    {
                        {"value", t},
                        {"label", t}
                    }).ToList();

                // Would like to update value in either table -- "StudentCourseTest" or "TeacherCourseTest"
                // Only "StudentCourseTest" edits stick
                var response = new Editor(db, "StudentCourseTest", "Id")
                    .Field(new Field("TeacherCourseTest.Teacher").Options(()=>teacherList))
                    .Field(new Field("TeacherCourseTest.CourseSectionId"))
                    .Field(new Field("StudentCourseTest.StudentName"))
                    .Field(new Field("StudentCourseTest.StudentId"))
                    .LeftJoin("TeacherCourseTest", "TeacherCourseTest.Id", "=", "StudentCourseTest.TeacherCourseTestId")
                    .Where("TeacherCourseTest.Campus", campus)
                    .Process(formData)
                    .Data();

                return Json(response, JsonRequestBehavior.AllowGet);
            }
        }

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Answer ✓

    Is it possible to apply edits to joined columns?

    Yes. You need to have the primary key value for the joined tables in the submitted data. They would usually be in a hidden field.

    Allan

This discussion has been closed.