Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
I have two models :
C#
public class StudentAddress
    {

       [ForeignKey ("stu")]
        [Key]
        public int StudentID { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public int ZipCode { get; set; }
        public virtual Student stu { get; set; }
    }


C#
public class Student
    {
        [Key]
        public int StudentID { get; set; }
        public string StudentName { get; set; }

        public string Address { get; set; }
        public int Age { get; set; }
        public int ClassID { get; set; }
        public virtual StudentAddress SAddresses { get; set; }

    }


I have created a edit page with all information from both the models and i am also able to populate the values in texbox too.
But now when i write my Edit post action in my controller I am clueless what is code in this action.

i am new to mvc and trying myself pl help me.
the code i tried is as follows:
C#
[HttpPost]

        public ActionResult EditWithEager(int id, Student stu)
        {
            try
            {

                var vardb = OBJ.Students
                   .Include(i => i.SAddresses)
                   .Where(i => i.StudentID == id)
                   .Single();

                if (TryUpdateModel(vardb, "",
                   new string[] { "StudentID", "StudentName","Age", "Address", "City","State", "ZipCode" }))
                {
                    try
                    {
                        OBJ.Entry(vardb).State = EntityState.Modified;
                        OBJ.SaveChanges();

                        return RedirectToAction("IndexWithEager");
                    }
                    catch
                    {
                         return View();
                    }
                }
                else
                {
                    return View();
                }
            }



Also when i see the stu in Edit action at runtime I am able to see values of student class but not of Address class.
Posted
Comments
[no name] 8-Oct-14 5:29am    
can you post thecode for the viewpage submit form ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900