Click here to Skip to main content
16,022,536 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have group model name Customer and this model Contain the nested model of CustomerDetail how can get the value of CustomerDetail which is nested inside the Main Customer Model on the Post Action in MVC Razor

What I have tried:

C#
class CustomerDetail
{
  public string Address{get;set;}
}


C#
class Customer
{
  public string Name{ get; set; }
  public string CustomerDetail{ get; set; }
}

On view

C#
@model Fred.WebApi.Models.mStripeCharge
@{
   @using (Html.BeginForm("ActionName", "ControllerName", new {  }, FormMethod.Post, new { }))
{
  @Html.HiddenFor(model => model.CustomerDetail)

}

}


but i am getting the value of model.CustomerDetail on Controller Action Null.

does it possible to preserve the value ?
Posted
Updated 6-Dec-16 3:04am
v2

1 solution

you have couple of things done wrong, first of all your models should be like:

C#
class CustomerDetail
{
   public string Address{get;set;}
}


class Customer
{

  public string Name{ get; set; }

  public CustomerDetail CustomerDetail{ get; set; }
}


and in your view you will need to create hidden fields for each CustomerDetail property like:

C#
 @using (Html.BeginForm("ActionName", "ControllerName", new {  }, FormMethod.Post, new { }))
{

  @Html.HiddenFor(model => model.CustomerDetail.Address)
 
}


This should work for you correctly.
 
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