TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Abbas Hamza
NA
100
53k
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
[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<Escort> Escorts {
get
;
set
; }
public
virtual
Driver Drivers{
get
;
set
; }
Here is my Driver:
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; }
}
here is my Razor view CreateDriver
@
using
(Html.BeginForm())
{
<fieldset>
<legend>
CreateDriver
</legend>
<div
class
=
"container-fluid"
>
<div
class
=
"row"
>
<div
class
=
"col-sm-6"
>
<div
class
=
"editor-label"
>
@Html.LabelFor(u => u.FirstName)
</div>
<div
class
=
"editor-field"
>
@Html.TextBoxFor(u => u.FirstName)
@Html.ValidationMessageFor(u => u.FirstName)
</div>
</div>
<div
class
=
"col-sm-6"
>
<div
class
=
"editor-label"
>
@Html.LabelFor(u => u.Surname)
</div>
<div
class
=
"editor-field"
>
@Html.TextBoxFor(u => u.Surname)
@Html.ValidationMessageFor(u => u.Surname)
</div>
</div>
</div>
<div
class
=
"row"
>
<div
class
=
"col-sm-6"
>
<div
class
=
"editor-label"
>
@Html.LabelFor(u => u.Contacts.Address1)
</div>
<div
class
=
"editor-field"
>
@Html.TextBoxFor(u => u.Contacts.Address1)
@Html.ValidationMessageFor(u => u.Contacts.Address1)
</div>
</div>
<div
class
=
"col-sm-6"
>
<div
class
=
"editor-label"
>
@Html.LabelFor(u => u.Contacts.Address2)
</div>
<div
class
=
"editor-field"
>
@Html.TextBoxFor(u => u.Contacts.Address2)
@Html.ValidationMessageFor(u => u.Contacts.Address2)
</div>
</div>
</div>
<div
class
=
"editor-label"
>
@Html.LabelFor(u => u.Contacts.Address3)
</div>
<div
class
=
"editor-field"
>
@Html.TextBoxFor(u => u.Contacts.Address3)
@Html.ValidationMessageFor(u => u.Contacts.Address3)
</div>
<div
class
=
"editor-label"
>
@Html.LabelFor(u => u.Crbt.CrbCheck)
</div>
<div
class
=
"editor-field"
>
@Html.PasswordFor(u => u.Crbt.CrbRef)
@Html.ValidationMessageFor(u => u.Crbt.CrbRef)
</div>
<div
class
=
"editor-label"
>
@Html.LabelFor(u => u.Crbt.Training)
</div>
<div
class
=
"editor-field"
>
@Html.TextBoxFor(u => u.Crbt.Training)
@Html.ValidationMessageFor(u => u.Crbt.Training);
</div>
<input type=
"submit"
value=
"Create driver"
/>
</div>
</fieldset>
}
Reply
Answers (
3
)
DropDownListFor unable to set on string (MVC)
Is it possible to get Azure trial subscription again ?