Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. HTTPPOST of EditReport not comiting data to database

HTTPPOST of EditReport not comiting data to database

Scheduled Pinned Locked Moved ASP.NET
asp-netdatabasecsharplinqxml
3 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Carl Cummings Canada
    wrote on last edited by
    #1

    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
    
    Richard DeemingR 1 Reply Last reply
    0
    • C Carl Cummings Canada

      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
      
      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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
      

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      C 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        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
        
        C Offline
        C Offline
        Carl Cummings Canada
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups