问题
The jquery remote validation is adding the prefix of the input field name (mymodel.field1) to each additional field listed in data-val-remote-additionalfields. In my additional fields I have a hidden field that is not part of the model so it has a normal name like "fieldhidden" instead of "mymodel.fieldhidden".
I have confirmed this by reviewing the request object at the controller and verified that one of the query string keys is "mymodel.fieldhidden" instead of "fieldhidden" and the data is null. Pretty sure its null because jquery validation is looking for "mymodel.fieldhidden" and of course can't find it.
Is there a way to make jquery, through mvc attributes, not auto prefix or through jquery manipulation to not auto prefix the additional fields where the name attribute has a value formatted like "model.fieldname" where remote validation is added ?
[Remote("ValidationMethod", "Controller", AdditionalFields = "FieldNameInModel, ElementNameNotInModel")]
public string FieldToRemoteValidate { get; set; }
回答1:
What I ended up having to do was add the hidden field twice. I already had a generic setup for multiple actions in the controller expecting the name as was. So by adding the hidden field with the naming convention that the jquery validation library expected, it was then transmitted as part of the query string. The naming convention is Model_Field for the id attribute and Model.Field for the name attribute.
I then used the bind parameter attribute with the prefix as follows:
[Bind(Prefix = "Model.FieldToRemotevalidate")] string FieldToRemoteValidate
来源:https://stackoverflow.com/questions/24948164/mvc-remote-attribute-additional-fields