Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 469.4k

How to save changes to db, using mutliple models in one view?

Jul 9 2020 6:43 AM
Hi team
 
I have multiple models in my View, the issue my form does not save to my db. I have before when used one it was saving well all changes to my db. Kindly please help me to improve my logic better and stuck.
 
  1. // Controller  
  2.        //GET:TrainingRegForm/Create/WebRequest.  
  3.         [HttpPost]  
  4.         [ValidateAntiForgeryToken]  
  5.         public ActionResult SubmitRegDetails([Bind(Include= "Id, Title, FirstName, LastName, Position, Company, StreetAddress, StreetAddressLine, City, StateProvince, ZipCode,Country,Email, CellNumber, DietaryRequirement")]TrainingRegForm eNtsaTraining)  
  6.         {  
  7.             this.ViewBag.SubmitRegDetails = this.SaveRegForm();  
  8.   
  9.             if(ModelState.IsValid)  
  10.             {  
  11.                 eNtsaTraining.Id = Guid.NewGuid();  
  12.                 db.TrainingRegs.Add(eNtsaTraining);  
  13.                 db.SaveChanges();  
  14.                 return RedirectToAction("SaveRegForm");  
  15.             }  
  16.   
  17.             // Validates when empty.  
  18.             if(ModelState.IsValid)  
  19.             {  
  20.                 return RedirectToAction("SaveRegForm");  
  21.             }  
  22.             return View(eNtsaTraining);  
  23.         }  
  24. // Data-Access-Layer  
  25. using System;  
  26. using System.Collections.Generic;  
  27. using System.Linq;  
  28. using System.Web;  
  29. using System.Data.Entity;  
  30. using eNtsaRegistrationTraining.Models;  
  31.   
  32. namespace eNtsaRegistrationTraining.DAL  
  33. {  
  34.     public class eNtsaRegistration:DbContext  
  35.     {  
  36.   
  37.         public eNtsaRegistration() : base("eNtsaRegistration")  
  38.         {  
  39.         }  public DbSet<TrainingRegForm> TrainingRegs { getset; }  
  40.   
  41.         protected override void OnModelCreating(DbModelBuilder modelBuilder)  
  42.         {  
  43.             base.OnModelCreating(modelBuilder);  
  44.         }  
  45.     }  
  46.  }  
  47.   
  48. //  Models  
  49. namespace eNtsaRegistrationTraining.Models  
  50. {  
  51.     public class TrainingRegForm  
  52.     {  
  53.         [Key]  
  54.         public Guid? Id { getset; }  
  55.         public string Title { getset; }  
  56.         public string FirstName { getset; }  
  57.         public string LastName { getset; }  
  58.         public string Position { getset; }  
  59.         public string Company { getset; }  
  60.   
  61.         public string StreetAddress { getset; }  
  62.   
  63.         public string StreetAddressLine { getset; }  
  64.   
  65.         public string City { getset; }  
  66.         public string StateProvince { getset; }  
  67.   
  68.         public int ZipCode { getset; }  
  69.   
  70.         public string Country { getset; }  
  71.   
  72.         public string Email { getset; }  
  73.   
  74.         [Required(ErrorMessage = "This field is required")]  
  75.         [DataType(DataType.PhoneNumber)]  
  76.         public string CellNumber { getset; }  
  77.         public string DietaryRequirement { getset; }  
  78.     }  
  79.   
  80.   
  81.     public class RegViewAndRoleViewModel  
  82.     {  
  83.         public DietViewModel DietMain { getset; }  
  84.   
  85.         public TrainingRegForm RegForm { getset; }  
  86.   
  87.        public DropDownViewModel ListCountries { getset; }  
  88.         public RegistrationTrainingForm HomeModel { getset; }  
  89.         public RoleViewModel RoleViewModelData { getset; }  
  90.   
  91.          
  92.     }  
  93. // View  
  94. @using eNtsaRegistrationTraining.Models  
  95. @model RegViewAndRoleViewModel  
  96. @{  
  97.     ViewBag.Title = "SubmitRegDetails";  
  98. }  
Its not sending to db, but if i am using one model(TbRegistrationForm only) it does save straight to my DB with all form fields. 

Answers (6)