How to bind html form value to a nested object property

时间:2018-02-26 17:43:26

标签: c# asp.net-core

I am pretty sure I did it before but I can't remember how. Do I have to use some annotation? As noted in the title I am using asp.net core.

I have a form element,

 //my attempt was like this.
  <input name="Address.Number" />

My action is,

public IActionResult Register(Client client)
{
      .....
}

The class Client has a nested complex object.

public class Client
{
    public Address address = new Address()
}

public class Address
{
    public int Number;
}

1 个答案:

答案 0 :(得分:0)

You need to use properties instead of fields:

public class Client
{
    public Address Address { get; set; } = new Address()
}

public class Address
{
    public int Number { get; set; }
}