Reputation: 99
Here,I have a strongly typed view in which I'm showing the registered user his/her data in a form which will allow for updating edited data and make those changes reflected back to database.
I have written all the code for the action methods in both the GET and POST method accordingly but i can't figure out what's causing the issue.The issue is that the changes i made on the view page binded to the model class are not reflected back to the database table though i have written the method db.submit changes().
Below is my code: GET and POST action methods for the Detailsupdate view page:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult PatientDetailsPage2(Patients p)
{
if (ModelState.IsValid)
{
tblPatient updatedpatientdetail = new tblPatient()
{
PatientName = p.PatientName,
PatientAge = (short) p.Age,
PatientMail = p.PatientEmail,
PatientMobileNo = p.PatientMobileNo,
PatientPassword = p.PatientPassword
};
db.SubmitChanges();
return View();
}
else
{
ViewBag.ErrorMessage = "Please ensure all the fields are filled correctly";
}
return View();
}
public ActionResult PatientDetailsPage2()
{
if(TempData["doc"] != null)
{
var data = (Patients)TempData["doc"];
return View(data);
}
return View();
}
Also I would mention that when i place debug and scan for updated values it shows the updated value at the point where the model's object is assigned into the table parameters but as soon as the submit changes line code is scanned it shows the old value for password field(the field value which i want to be updated here).Please help me programmers!
Upvotes: 1
Views: 442
Reputation: 1590
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult PatientDetailsPage2(Patients p)
{
if (ModelState.IsValid)
{
tblPatient updatedpatientdetail = new tblPatient()
{
PatientName = p.PatientName,
PatientAge = (short) p.Age,
PatientMail = p.PatientEmail,
PatientMobileNo = p.PatientMobileNo,
PatientPassword = p.PatientPassword
};
db.Patients.Add(updatedpatientdetail);
db.SubmitChanges();
return View();
}
else
{
ViewBag.ErrorMessage = "Please ensure all the fields are filled correctly";
}
return View();
}
you must add your model object to Db Model before save.
Upvotes: 1