I have a class "Client" and "Address". Each Client can have more then one address i.e. Postal and Residential.
I have created ModelView of them to use for data entry in razor: MODEL VIEW:
Public class Client
{
public int ClientID { get; set; }
public string ClientName {get;set;}
}
Public class Addresses
{
public int ClientID { get; set; }
public int AddressType {get;set;}
public string Addr {get;set;}
}
Public class ClientsInfoModelView
{
public Client clnt{get;set;}
public List<Addresses> addr {get;set;}
}
Controller (ClientsController):
Public ActionResult Index()
{
ClientsInfoModelView _MV = new ClientsInfoModelView();
_MV.clnt = Context.Client.FirstOrDefault(w=> w.ClientID=1);
_MV.Addr = Context.Addresses.FirstOrDefault(w=> w.ClientID=1);
return View("index",_MV);
}
Public ActionResult SAVE(ClientsInfoModelView data)
{
// here data.addr is null, where as clnt has data as per filled in the form
}
Razor
@model ModelViews. ClientsInfoModelView
@using (Html.beginform(“SAVE”,”Clients”,FormMethod.post))
{
@html.TextBoxFor(m=> m. ClientID);
@html.TextBoxFor(m=> m. ClientName);
Address:
Postal: @html.TextBoxFor(m=> m. addr(w=> w. AddressType==1). Addr)
Residential: @html.TextBoxFor(m=> m. addr(w=> w. AddressType==2). Addr)
<button type=”submit”> Save </button>
}
Display works fine. I can see the data populated for Client as well as the address but Issue arises where I click save button . Controller is receiving null for Addr property, which is referring List.