I have used a pattern of compound view models for a few of the forms in this project. It works very well.
In this case I have a VendorAddress view model. I use Add
I ran into this issue, but needed to use the same ViewModel in multiple views with different prefixes. I ended up writing javascript that modified the jQuery remote validation rules so they did not include any prefix:
$('[data-val-remote]').each(function () {
// overwrite the remote rule data so that it sends non-prefixed property name to the server
var remoteRule = $(this).rules().remote;
var newData = {};
var keys = Object.keys(remoteRule.data);
for (var i = 0; i < keys.length; i++) {
var nonPrefixedPropertyName = $(keys[i].split('.')).last()[0];
newData[nonPrefixedPropertyName] = remoteRule.data[keys[i]];
}
remoteRule.data = newData;
});
Try specifying a prefix to help the model binder correctly bind the Address.PostalCode
and Address.State
request values to their corresponding action parameters:
public ActionResult CheckState(
[Bind(Prefix = "Address.State")]string State,
[Bind(Prefix = "Address.PostalCode")]string PostalCode
)
{
...
}