HTTPPOST of EditReport not comiting data to database
-
Hi All I am new to MVC and EntityFramework and am having a problem getting my form to save changes back to the database. I have the following model
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Services.Providers;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace HAZID.Core.Models
{
public class HAZID_Forms : HAZID_Base_Entity
{\[Display(Name = "HAZID\_Workers\_Id", ResourceType = typeof(Resources.Resource))\] \[Required(ErrorMessageResourceName = "HAZID\_Workers\_Id\_Error\_Msg", ErrorMessageResourceType = typeof(Resources.Resource))\] public int HAZID\_Workers\_Id { get; set; } \[Display(Name = "HAZID\_Supervisors\_Id", ResourceType = typeof(Resources.Resource))\] \[Required(ErrorMessageResourceName = "HAZID\_Supervisor\_Id\_Error\_Msg", ErrorMessageResourceType = typeof(Resources.Resource))\] public int HAZID\_Supervisors\_Id { get; set; } \[Display(Name = "HAZID\_Form\_DateTime", ResourceType = typeof(Resources.Resource))\] public DateTime HAZID\_Form\_DateTime { get; set; } \[Display(Name = "HAZID\_District\_Id", ResourceType = typeof(Resources.Resource))\] \[Required(ErrorMessageResourceName = "HAZID\_District\_Id\_Error\_Msg", ErrorMessageResourceType = typeof(Resources.Resource))\] public int HAZID\_District\_Id { get; set; } \[Display(Name = "HAZID\_Hazard\_Location", ResourceType = typeof(Resources.Resource))\] \[DataType(DataType.MultilineText)\] \[Required(ErrorMessageResourceName = "HAZID\_Hazard\_Location\_Error\_Msg", ErrorMessageResourceType = typeof(Resources.Resource))\] public string HAZID\_Hazard\_Location { get; set; } \[Display(Name = "HAZID\_Hazard\_Description", ResourceType = typeof(Resources.Resource))\] \[DataType(DataType.MultilineText)\] \[Required(ErrorMessageResourceName = "HAZID\_Hazard\_Description\_Error\_Msg", ErrorMessageResourceType = typeof(Resources.Resource))\] public string HAZID\_Hazard\_Description { get; set; } public string HAZID\_Hazard\_Description\_Short { get { return HAZID\_Hazard\_Description.ToString().SubStringTo(30); } } \[Display(Name = "HAZID\_Workers\_Suggstion", ResourceType = typeof(Resources.Resource))\] \[DataType(DataType.MultilineText)\] public string HAZID\_Workers\_Suggstion { get; set; } pu
-
Hi All I am new to MVC and EntityFramework and am having a problem getting my form to save changes back to the database. I have the following model
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Services.Providers;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace HAZID.Core.Models
{
public class HAZID_Forms : HAZID_Base_Entity
{\[Display(Name = "HAZID\_Workers\_Id", ResourceType = typeof(Resources.Resource))\] \[Required(ErrorMessageResourceName = "HAZID\_Workers\_Id\_Error\_Msg", ErrorMessageResourceType = typeof(Resources.Resource))\] public int HAZID\_Workers\_Id { get; set; } \[Display(Name = "HAZID\_Supervisors\_Id", ResourceType = typeof(Resources.Resource))\] \[Required(ErrorMessageResourceName = "HAZID\_Supervisor\_Id\_Error\_Msg", ErrorMessageResourceType = typeof(Resources.Resource))\] public int HAZID\_Supervisors\_Id { get; set; } \[Display(Name = "HAZID\_Form\_DateTime", ResourceType = typeof(Resources.Resource))\] public DateTime HAZID\_Form\_DateTime { get; set; } \[Display(Name = "HAZID\_District\_Id", ResourceType = typeof(Resources.Resource))\] \[Required(ErrorMessageResourceName = "HAZID\_District\_Id\_Error\_Msg", ErrorMessageResourceType = typeof(Resources.Resource))\] public int HAZID\_District\_Id { get; set; } \[Display(Name = "HAZID\_Hazard\_Location", ResourceType = typeof(Resources.Resource))\] \[DataType(DataType.MultilineText)\] \[Required(ErrorMessageResourceName = "HAZID\_Hazard\_Location\_Error\_Msg", ErrorMessageResourceType = typeof(Resources.Resource))\] public string HAZID\_Hazard\_Location { get; set; } \[Display(Name = "HAZID\_Hazard\_Description", ResourceType = typeof(Resources.Resource))\] \[DataType(DataType.MultilineText)\] \[Required(ErrorMessageResourceName = "HAZID\_Hazard\_Description\_Error\_Msg", ErrorMessageResourceType = typeof(Resources.Resource))\] public string HAZID\_Hazard\_Description { get; set; } public string HAZID\_Hazard\_Description\_Short { get { return HAZID\_Hazard\_Description.ToString().SubStringTo(30); } } \[Display(Name = "HAZID\_Workers\_Suggstion", ResourceType = typeof(Resources.Resource))\] \[DataType(DataType.MultilineText)\] public string HAZID\_Workers\_Suggstion { get; set; } pu
Member 15016778 wrote:
HAZID_Forms hazidFormToEdit = hazidForms.Find(id);
...
hazidFormToEdit = hazidFormViewModel.HAZID_Form;
hazidForms.Commit();Your code finds the form with the specified ID, and stores it in a local variable. It then overwrites the local variable with the value from the view-model, breaking any link between the variable and the repository. It then tells the repository to commit the changes. But there are no changes to commit, because you've not updated anything that the repository knows about. Instead of overwriting the local variable, you need to update the entity returned from the
Find
method using the properties of the view-model. You'll also need to repopulate the view-model collections before displaying the view again.private void PopulateLookups(HAZID_Form_View_Model hazidFormViewModel)
{
hazidFormViewModel.HAZID_Branch_Districts = hazidBranchDistricts.Collection();
hazidFormViewModel.HAZID_Hazard_Types = hazidHazardTypes.Collection();
hazidFormViewModel.HAZID_Risk_Severity_Types = hazidRiskSeverityTypes.Collection();
hazidFormViewModel.HAZID_Risk_Probability_Types = hazidRiskProbabilityTypes.Collection();
hazidFormViewModel.HAZID_Statuses = hazidStatuses.Collection();
hazidFormViewModel.HAZID_Persons = hazidPersons.Collection();
}[HttpGet]
public ActionResult EditReport(int id)
{
HAZID_Forms hazidFormToEdit = hazidForms.Find(id);
if (hazidFormToEdit == null)
{
return HttpNotFound();
}HAZID\_Form\_View\_Model hazidFormViewModel = new HAZID\_Form\_View\_Model(); hazidFormViewModel.HAZID\_Form = hazidFormToEdit; PopulateLookups(hazidFormViewModel); return View(hazidFormViewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditReport(HAZID_Form_View_Model hazidFormViewModel, int id)
{
HAZID_Forms hazidFormToEdit = hazidForms.Find(id);
if (hazidFormToEdit == null)
{
return HttpNotFound();
}if (!ModelState.IsValid) { PopulateLookups(hazidFormViewModel); return View(hazidFormViewModel); } // Copy the properties from the view-model to the entity: hazidFormToEdit.HAZID\_Status\_Id = hazidFormViewModel.HAZID\_Form.HAZID\_Status\_Id; hazidFormToEdit.HAZID\_Action\_Taken = hazidFormViewModel.HAZID\_Form.HAZID\_Action\_Taken; ... hazidForms.Commit(); return Redi
-
Member 15016778 wrote:
HAZID_Forms hazidFormToEdit = hazidForms.Find(id);
...
hazidFormToEdit = hazidFormViewModel.HAZID_Form;
hazidForms.Commit();Your code finds the form with the specified ID, and stores it in a local variable. It then overwrites the local variable with the value from the view-model, breaking any link between the variable and the repository. It then tells the repository to commit the changes. But there are no changes to commit, because you've not updated anything that the repository knows about. Instead of overwriting the local variable, you need to update the entity returned from the
Find
method using the properties of the view-model. You'll also need to repopulate the view-model collections before displaying the view again.private void PopulateLookups(HAZID_Form_View_Model hazidFormViewModel)
{
hazidFormViewModel.HAZID_Branch_Districts = hazidBranchDistricts.Collection();
hazidFormViewModel.HAZID_Hazard_Types = hazidHazardTypes.Collection();
hazidFormViewModel.HAZID_Risk_Severity_Types = hazidRiskSeverityTypes.Collection();
hazidFormViewModel.HAZID_Risk_Probability_Types = hazidRiskProbabilityTypes.Collection();
hazidFormViewModel.HAZID_Statuses = hazidStatuses.Collection();
hazidFormViewModel.HAZID_Persons = hazidPersons.Collection();
}[HttpGet]
public ActionResult EditReport(int id)
{
HAZID_Forms hazidFormToEdit = hazidForms.Find(id);
if (hazidFormToEdit == null)
{
return HttpNotFound();
}HAZID\_Form\_View\_Model hazidFormViewModel = new HAZID\_Form\_View\_Model(); hazidFormViewModel.HAZID\_Form = hazidFormToEdit; PopulateLookups(hazidFormViewModel); return View(hazidFormViewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditReport(HAZID_Form_View_Model hazidFormViewModel, int id)
{
HAZID_Forms hazidFormToEdit = hazidForms.Find(id);
if (hazidFormToEdit == null)
{
return HttpNotFound();
}if (!ModelState.IsValid) { PopulateLookups(hazidFormViewModel); return View(hazidFormViewModel); } // Copy the properties from the view-model to the entity: hazidFormToEdit.HAZID\_Status\_Id = hazidFormViewModel.HAZID\_Form.HAZID\_Status\_Id; hazidFormToEdit.HAZID\_Action\_Taken = hazidFormViewModel.HAZID\_Form.HAZID\_Action\_Taken; ... hazidForms.Commit(); return Redi
Thanks soo much that makes since. I just thought i could copy the whole thing in one step but now i see that is not the case. As for the raw i was once again not sure as I need to get the text from a different column based on the language. If you know a better way i would appreciate any insight.