Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


SQL
//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
Updated 20-Mar-13 2:28am
v2
Comments
lakshmi priya iyer 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??
[no name] 20-Mar-13 8:28am    
k

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 :




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..
 
Share this answer
 
Comments
[no name] 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 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 :)
[no name] 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..
Your Models

C#
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)






}

@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).


C#
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();
    }

}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900