Click here to Skip to main content
11,579,225 members (73,945 online)
Rate this: bad
good
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 ;
}
 
[HttpPost]
Public ActionResult Create (Employee Emp1)
{
 
 Billing_Address b=  Emp1.Billing_Address ;
  
 
}
/// Here value of B is always null





Thanks
Posted 20-Mar-13 2:20am
Edited 20-Mar-13 2:28am
v2
Comments
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
   
k
Rate this: bad
good
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 :
[HttpPost]
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>
  Permalink  
Comments
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
good
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";
}

Add
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)


Employee

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


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


BillingAddress

@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 {
@Scripts.Render("~/bundles/jqueryval")
}

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();
        }
 
        [HttpPost]
        public ActionResult Add(FormAuth.Models.Employee employee)
        {
            return View();
        }
 
    }
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 1,003
1 Sergey Alexandrovich Kryukov 750
2 Abhinav S 583
3 F-ES Sitecore 420
4 Dave Kreskowiak 419


Advertise | Privacy | Mobile
Web04 | 2.8.150603.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