Id column from database model shows as null

Id column from database model shows as null

rdmrdm Posts: 194Questions: 55Answers: 4

I am learning how to use Editor in a .NET MVC project and am unable to solve an issue.

My database table has a primary key field called "Id".

While editor shows all of the other fields in my table with their correct values, the Id is consistently null.

What do I need to change so that Id is not null?
On a related note, changes I make on the inline editor do not persist on the database. My theory was that the null Id was part of it -- however as I receive no compiler, runtime, nor browser errors, I am not sure how to proceed.

Thank you

Controller Action

public ActionResult Table()
        {
            var settings = Properties.Settings.Default;
            var formData = HttpContext.Request.Form;

            using (var db = new Database(settings.DbType, settings.DbConnection))
            {
                DtResponse response;
                try
                {
                    response = new Editor(db, "TestTable")
                        .Model<TestTable>()
                        .Field(new Field("Id"))
                        .Field(new Field("Campus"))
                        .Field(new Field("Teacher"))
                        .Field(new Field("CourseSectionId"))
                        .Process(formData)
                        .Data();
                    
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
                return Json(response, JsonRequestBehavior.AllowGet);
            }
        }

Model

public class TestTable
    {
        public int Id { get; set; }
        public string Campus { get; set; }
        public string Teacher { get; set; }
        public string CourseSectionId { get; set; }
    }

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,946Questions: 1Answers: 10,158 Site admin
    edited September 2017 Answer ✓

    Change:

    response = new Editor(db, "TestTable")

    to be:

    response = new Editor(db, "TestTable", "Id")
    

    and remove:

    .Field(new Field("Id"))

    and

    public int Id { get; set; }

    You don't need to include the primary key column as a field - just having it as the primary key is enough.

    See also the documentation about primary keys here.

    Allan

This discussion has been closed.