ASP.NET MVC GetFullHtmlFieldId not returing valid id

你离开我真会死。 提交于 2019-12-06 10:50:49

It seems that the valid prefix depends on the expressions used to build the page, and that isn't available to the GetClientValidationRules method. It would be great if someone has a solution for it, but as the method GetClientValidationRules is trying to initialize the data needed for the Javascript validation, you could try to resolve this on the client side.

On some of the default validation attributes like the [Compare], which depends on a second field, they set a parameter *.propertyName and in the unobtrusive adapter they replace the *. part with the valid prefix retrieved from the input name attribute. You could try a similar approach for the id.

However this would be needed if you were interested in another field. In this case it seems you are interested in the id of the very same input field that you are validating. You could then retrieve it from the input element itself. It will be available in both the unobtrusive adapter or the method itself:

//You could get it in the adapter and pass it to the validator in the "param"
adapters.add("dummy", function (options) {
    var fullFieldId = options.element.id;
    if (console) console.log("Full field id: " + fullFieldId);
    setValidationValues(options, "dummy", fullFieldId);
});
$jQval.addMethod("dummy", function (value, element, param) {
    var fullFieldId = param;
    //do whatever validation logic
    return true;
});

//You could also retrieve it directly in the validator method
$jQval.addMethod("dummy", function (value, element, param) {
    var fullFieldId = element.id;
    //do whatever validation logic
    return true;
});
adapters.add("dummy", function (options) {
    setValidationValues(options, "dummy", {});
});

Hope it helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!