I am learning ASP MVC and I have problem. I have class Company with an aggregated property:
public class Company
{
public Company()
{
Address = new Address();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual Address Address { get; set; }
}
And Address class:
public class Address
{
public int Id { get; set; }
public string Country { get; set; }
public string City { get; set; }
...
}
How should I write edit for this field? I have tried to create control AddressDetail and pass it to UiHint attribute on Address property, but in view i have additional Id property to edit (!?). When I hide it model state in post is not valid because this property is required.
Another question is how should repository look like for Company? Should it use AddressRepository somehow or write Address by itself?
Maybe somebody have example of that kind of situation?
As long as the Address class has a parameterless constructor (like your company class), the default mvc modelbinder will have no problem handeling your object. It is not a problem, that both your Company class and Address class has an Id property.
MVC will by default assign your inputfields with names like ‘Id’ and ‘Address.Id’, which allows for the modelbinder to distinguish between the two properties.
You could register an editor template for your address class in Global.asax, or you could simple make a partial view and pass it Company.Address in your main view.