Consider I have the following classes as ViewModels
public class Address
{
public String Street { get; set; }
}
public class Person()
{
public Address Address { get; set; }
}
If I have a View which Model is Person, I can do this:
@Html.EditorFor(model => model.Address.Street)
The resulting input name will be Address.Street and, when I post it to an Action, the default model binder is able to realize that the input which name is Address.Street actually refers to the property Street inside the property Address.
That works fine.
However, I have a problem when I try to use this:
this.Url.Action("MyAction", myPerson)
I expect the Url serializer to build a queryString like this: /person/edit?Address.Street=X but it actually does’t serialize the Address property.
Is there something I can do to make the Url serializer to work in this scenario?
I think there is no default solution for it because Url.Actoin internally create RouteValueDictionary from object that you specify (in your case it myPerson). And RouteValueDictionary fill key/value pairs using following method :
And as result for address you get following pair { “Address”, person.Address }
And finally route just convert value to string and add it to query string and as result in query string you get ?Adress=[person.Address.ToString()])
I think the simplest way to do it is write a helper which will build RouteValueDictionary from object in way that you need.