Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: MVC MVC3 MVC2 MVC4
Hi Everybody,
I have Create a Model in MVC 4 Razor.
public class Employee
public int employeeid {get;set;}
public Name EmployeeName {get;set;}
public Billing_Address EmpBillingAddress {get;set;}
I have create a View EmployeeCreation in which have i am binding this view to Employee object.
but when i am submit this view . In post i am unable to get the values of Billing_Address Object value.

So i need some co ordinations for the same topics. Because there are some more Properties which are class object properties.

//MyController Code

Public ActionResult Create ()
   return view ;
Public ActionResult Create (Employee Emp1)
 Billing_Address b=  Emp1.Billing_Address ;
/// Here value of B is always null


Posted 20-Mar-13 3:20am
Edited 20-Mar-13 3:28am
lakshmi priya iyer at 20-Mar-13 8:22am
Please post ur complete code. Because you have to say if that object value u want to get in post is via what? As in, do u get it from textbox after entering and submitting??
eg_Anubhava at 20-Mar-13 8:28am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Initially any object/property when created will be null. after post it will be the same. Whatever values have been submitted/posted in view only will be returned to the controller after a post action. for example,
In View :
<input id="txtaddress" type="text" name="EmpBillingAddress" class="tcal" value="test">

In controller :
Public ActionResult Create (Employee Emp1)
Billing_Address b= Emp1.EmpBillingAddress;

P.S : Change the name. Because in the Employee class, there is a property EmpBillingAddress.
I think that is why it is showing null. Change it and try. You are once again trying to access the class and not the property. Please tell if it solves..</input>
eg_Anubhava at 20-Mar-13 8:42am
Yes Lakshmi i agree with you but I can't change my project architecture. EmpBillingAddress Class have many fields . so it there any other option by using i can get the object or value of this object.
lakshmi priya iyer at 20-Mar-13 8:50am
May be you could use Emp1.Billing_Address.EmpBillingAddress might work out. Not sure !! just a logical suggestion !! It might work :)
eg_Anubhava at 21-Mar-13 11:49am
No .. It's Not working ... When i am trying to submit the View then Emp1 variable found the value of EmpBillingAddress is null.....
Pls try and find the solution and provide us to the same..
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Your Models
    public class Employee
        public int ID { get; set; }
        public String Name { get; set; }
        public BillingAddress billingAddress { get; set; }
    public class BillingAddress
        public int ID { get; set; }
        public int HouseNo { get; set; }
        public String Street { get; set; }
        public String Locality { get; set; }
        public String City { get; set; }
        public String District { get; set; }
        public String State { get; set; }
        public String Country { get; set; }
        public int PinCode { get; set; }
And View
@model FormAuth.Models.Employee
ViewBag.Title = "Add";
@using (Html.BeginForm())


@Html.LabelFor(model => model.Name)

@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)


@Html.LabelFor(model => model.billingAddress.HouseNo)

@Html.EditorFor(model => model.billingAddress.HouseNo)
@Html.ValidationMessageFor(model => model.billingAddress.HouseNo)

@Html.LabelFor(model => model.billingAddress.Street)

@Html.EditorFor(model => model.billingAddress.Street)
@Html.ValidationMessageFor(model => model.billingAddress.Street)

@Html.LabelFor(model => model.billingAddress.Locality)

@Html.EditorFor(model => model.billingAddress.Locality)
@Html.ValidationMessageFor(model => model.billingAddress.Locality)

@Html.LabelFor(model => model.billingAddress.City)

@Html.EditorFor(model => model.billingAddress.City)
@Html.ValidationMessageFor(model => model.billingAddress.City)

@Html.LabelFor(model => model.billingAddress.District)

@Html.EditorFor(model => model.billingAddress.District)
@Html.ValidationMessageFor(model => model.billingAddress.District)

@Html.LabelFor(model => model.billingAddress.State)

@Html.EditorFor(model => model.billingAddress.State)
@Html.ValidationMessageFor(model => model.billingAddress.State)

@Html.LabelFor(model => model.billingAddress.Country)

@Html.EditorFor(model => model.billingAddress.Country)
@Html.ValidationMessageFor(model => model.billingAddress.Country)

@Html.LabelFor(model => model.billingAddress.PinCode)

@Html.EditorFor(model => model.billingAddress.PinCode)
@Html.ValidationMessageFor(model => model.billingAddress.PinCode)

<input type="submit" value="Create" />

@Html.ActionLink("Back to List", "Index")
@section Scripts {
you can write your view in HTML syntax only, instead of razor syntax. But take care of name of the controls (As Laxmi Said).

    public class EmployeeController : Controller
        // GET: /Emplyee/

        public ActionResult Index()
            return View();
        public ActionResult Add()
            return View();
        public ActionResult Add(FormAuth.Models.Employee employee)
            return View();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 449
1 Maciej Los 430
2 OriginalGriff 320
3 CHill60 240
4 CPallini 210
0 Sergey Alexandrovich Kryukov 10,102
1 OriginalGriff 9,495
2 Peter Leow 5,241
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 3,076

Advertise | Privacy | Mobile
Web03 | 2.8.150327.1 | Last Updated 26 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100