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
- [HttpPost]
- public ActionResult CreateDriver(Driver driver)
- {
- using (var db = new localDBEntities())
- {
- var newDriver = db.Drivers.Create();
- newDriver.FirstName = driver.FirstName;
- newDriver.Surname = driver.Surname;
- newDriver.BadgeNo = driver.BadgeNo;
- newDriver.Contacts = driver.Contacts;
- // newDriver.c
- }
- return View(driver);
- }
here is my Contact Model
- public class Contact
- {
- public int ContactId { get; set; }
- [Display(Name = "House name/ number:")]
- public string Address1 { get; set; }
- [Display(Name = "Street name:")]
- public string Address2 { get; set; }
- [Display(Name = "Town:")]
- public string Address3 { get; set; }
- public string City { get; set; }
- public string Postcode { get; set; }
- public string Telephone { get; set; }
- public string Email { get; set; }
- public DateTime DateOfBirth { get; set; }
- public string NationalInsuranceNo { get; set; }
- public virtual ICollection
Escorts { get; set; } - public virtual Driver Drivers{ get; set; }
- public class Driver
- {
- Contact contact = new Contact();
- CrbTraining crbt = new CrbTraining();
- public CrbTraining Crbt
- {
- get { return this.crbt; }
- set { crbt = value; }
- }
- public int DriverId { get; set; }
- [Display(Name = "First name:")]
- public string FirstName { get; set; }
- [Display(Name = "Surname:")]
- public string Surname { get; set; }
- [Display(Name="Badge number:")]
- public int BadgeNo { get; set; }
- public Contact Contacts
- {
- get { return this.contact; }
- set { contact = value; }
- }
- @using (Html.BeginForm())
- {
- CreateDriver
- class="container-fluid">class="row">class="col-sm-6">class="editor-label">
- @Html.LabelFor(u => u.FirstName)
class="editor-field">- @Html.TextBoxFor(u => u.FirstName)
- @Html.ValidationMessageFor(u => u.FirstName)
class="col-sm-6">class="editor-label">- @Html.LabelFor(u => u.Surname)
class="editor-field">- @Html.TextBoxFor(u => u.Surname)
- @Html.ValidationMessageFor(u => u.Surname)
class="row">class="col-sm-6">class="editor-label">- @Html.LabelFor(u => u.Contacts.Address1)
class="editor-field">- @Html.TextBoxFor(u => u.Contacts.Address1)
- @Html.ValidationMessageFor(u => u.Contacts.Address1)
class="col-sm-6">class="editor-label">- @Html.LabelFor(u => u.Contacts.Address2)
class="editor-field">- @Html.TextBoxFor(u => u.Contacts.Address2)
- @Html.ValidationMessageFor(u => u.Contacts.Address2)
class="editor-label">- @Html.LabelFor(u => u.Contacts.Address3)
class="editor-field">- @Html.TextBoxFor(u => u.Contacts.Address3)
- @Html.ValidationMessageFor(u => u.Contacts.Address3)
class="editor-label">- @Html.LabelFor(u => u.Crbt.CrbCheck)
class="editor-field">- @Html.PasswordFor(u => u.Crbt.CrbRef)
- @Html.ValidationMessageFor(u => u.Crbt.CrbRef)
class="editor-label">- @Html.LabelFor(u => u.Crbt.Training)
class="editor-field">- @Html.TextBoxFor(u => u.Crbt.Training)
- @Html.ValidationMessageFor(u => u.Crbt.Training);
- "submit" value="Create driver" />
- }
Bikesh SrivastavaPosted Jan 26, 2017, 1:57 PM
Abbas HamzaPosted Jan 27, 2017, 5:58 AM
Abbas HamzaPosted Jan 26, 2017, 2:10 PM