hi all,
I have two models :
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; }
}
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:
[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.