Compound View Model object causing remote validation failure

后端 未结 2 1623
庸人自扰
庸人自扰 2021-01-05 06:14

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

相关标签:
2条回答
  • 2021-01-05 06:29

    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;
    });
    
    0 讨论(0)
  • 2021-01-05 06:35

    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
    )
    {
        ...
    }
    
    0 讨论(0)
提交回复
热议问题