Abbas Hamza

Abbas Hamza

  • NA
  • 100
  • 52.8k

accessing a model properties from another model in MVC

Jan 26 2017 5:46 AM
Hi Guys I have an mvc web application in the model i have driver model and Contact model i have created a contact property in the Driver model as i want to save only names and few things but the contact model is used by other model as well. i have created a DriverController in which i want to save details entered by user including details for the driver first name , ...etc as well as some data for the contact model including address details, the issue is when i try to access the contact information from the driver property fo it in the Driver model i couldn't access ant but i was able to acces it from the view! the first question how i would be able to do that? the second question is how do i save both at the same time into database bare in mind that Contact model got a driverID as a foreignkey so i need to be able the newly created DriverID from driver(automatically generated) to contact table I'm using MVC4 and EF6
here is my controller
 
  1. [HttpPost]  
  2.        public ActionResult CreateDriver(Driver driver)  
  3.        {  
  4.            using (var db = new localDBEntities())  
  5.            {  
  6.                var newDriver = db.Drivers.Create();  
  7.   
  8.                newDriver.FirstName = driver.FirstName;  
  9.                newDriver.Surname = driver.Surname;  
  10.                newDriver.BadgeNo = driver.BadgeNo;  
  11.                newDriver.Contacts = driver.Contacts;  
  12.               // newDriver.c  
  13.                 
  14.                
  15.                 
  16.            }  
  17.   
  18.            return View(driver);  
  19.        }  
here is my Contact Model
  1. public class Contact  
  2.     {  
  3.           
  4.         public int ContactId { getset; }  
  5.         [Display(Name = "House name/ number:")]  
  6.         public string Address1 { getset; }  
  7.         [Display(Name = "Street name:")]  
  8.         public string Address2 { getset; }  
  9.         [Display(Name = "Town:")]  
  10.         public string Address3 { getset; }  
  11.         public string City { getset; }  
  12.   
  13.         public string Postcode { getset; }  
  14.   
  15.         public string Telephone { getset; }  
  16.   
  17.         public string Email { getset; }  
  18.   
  19.         public DateTime DateOfBirth { getset; }  
  20.   
  21.         public string NationalInsuranceNo { getset; }  
  22.   
  23.   
  24.         public virtual ICollection<Escort> Escorts { getset; }  
  25.         public virtual  Driver Drivers{ getset; }  
 Here is my Driver:
  1. public class Driver  
  2.     {  
  3.   
  4.         Contact contact = new Contact();  
  5.         CrbTraining crbt = new CrbTraining();  
  6.   
  7.         public CrbTraining Crbt  
  8.         {  
  9.             get { return this.crbt; }  
  10.             set { crbt = value; }  
  11.         }  
  12.                          
  13.         public int DriverId { getset; }  
  14.         [Display(Name = "First name:")]  
  15.         public string FirstName { getset; }  
  16.   
  17.         [Display(Name = "Surname:")]  
  18.         public string Surname { getset; }  
  19.   
  20.         [Display(Name="Badge number:")]  
  21.         public int BadgeNo { getset; }  
  22.   
  23.         public Contact Contacts  
  24.         {  
  25.             get { return this.contact; }  
  26.             set { contact = value; }  
  27.         }  
 here is my Razor view CreateDriver
  1. @using (Html.BeginForm())  
  2. {  
  3.     <fieldset>  
  4.         <legend>  
  5.             CreateDriver  
  6.         </legend>  
  7.         <div class="container-fluid">  
  8.             <div class="row">  
  9.                 <div class="col-sm-6">  
  10.                     <div class="editor-label">  
  11.                         @Html.LabelFor(u => u.FirstName)  
  12.                     </div>  
  13.                     <div class="editor-field">  
  14.                         @Html.TextBoxFor(u => u.FirstName)  
  15.                         @Html.ValidationMessageFor(u => u.FirstName)  
  16.                     </div>  
  17.                 </div>  
  18.   
  19.                 <div class="col-sm-6">  
  20.                     <div class="editor-label">  
  21.                         @Html.LabelFor(u => u.Surname)  
  22.                     </div>  
  23.                     <div class="editor-field">  
  24.                         @Html.TextBoxFor(u => u.Surname)  
  25.                         @Html.ValidationMessageFor(u => u.Surname)  
  26.                     </div>  
  27.                 </div>  
  28.             </div>  
  29.   
  30.               
  31.             <div class="row">  
  32.                 <div class="col-sm-6">  
  33.                     <div class="editor-label">  
  34.                         @Html.LabelFor(u => u.Contacts.Address1)  
  35.                     </div>  
  36.   
  37.   
  38.                     <div class="editor-field">  
  39.                         @Html.TextBoxFor(u => u.Contacts.Address1)  
  40.                         @Html.ValidationMessageFor(u => u.Contacts.Address1)  
  41.                     </div>  
  42.                     </div>  
  43.                   
  44.                 <div class="col-sm-6">  
  45.                     <div class="editor-label">  
  46.                         @Html.LabelFor(u => u.Contacts.Address2)  
  47.                     </div>  
  48.                     <div class="editor-field">  
  49.                         @Html.TextBoxFor(u => u.Contacts.Address2)  
  50.                         @Html.ValidationMessageFor(u => u.Contacts.Address2)  
  51.                     </div>  
  52.                     </div>  
  53.                 </div>  
  54.                         <div class="editor-label">  
  55.                             @Html.LabelFor(u => u.Contacts.Address3)  
  56.                         </div>  
  57.                         <div class="editor-field">  
  58.                             @Html.TextBoxFor(u => u.Contacts.Address3)  
  59.                             @Html.ValidationMessageFor(u => u.Contacts.Address3)  
  60.                         </div>  
  61.   
  62.                         <div class="editor-label">  
  63.                             @Html.LabelFor(u => u.Crbt.CrbCheck)  
  64.                         </div>  
  65.                         <div class="editor-field">  
  66.                             @Html.PasswordFor(u => u.Crbt.CrbRef)  
  67.                             @Html.ValidationMessageFor(u => u.Crbt.CrbRef)  
  68.                         </div>  
  69.                         <div class="editor-label">  
  70.                             @Html.LabelFor(u => u.Crbt.Training)  
  71.                         </div>  
  72.                         <div class="editor-field">  
  73.                             @Html.TextBoxFor(u => u.Crbt.Training)  
  74.                             @Html.ValidationMessageFor(u => u.Crbt.Training);  
  75.                         </div>  
  76.                         <input type="submit" value="Create driver" />  
  77.                     </div>  
  78. </fieldset>  
  79. }  
 

Answers (3)