ASP.NET MVC GetFullHtmlFieldId不重设有效的ID [英] ASP.NET MVC GetFullHtmlFieldId not returing valid id

查看:92
本文介绍了ASP.NET MVC GetFullHtmlFieldId不重设有效的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了一下,但是对我没有帮助

I have taken a look at but it did not help me out

  1. GetFullHtmlFieldId返回不正确的ID属性值
  2. ASP.NET GetFullHtmlFieldId没有返回有效ID
  1. GetFullHtmlFieldId returning incorrect id attribute value
  2. ASP.NET GetFullHtmlFieldId not returning valid id

问题

基本上我有以下问题:

Problem

Basically I have the following problem:

我有一个自定义验证属性,该属性需要获取控件的fieldId

I have a custom validation attribute which requires to get the fieldId of the control

public class MyValidationAttribute : ValidationAttribute, IClientValidatable
{
    //...... Collapsed code
    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        ViewContext vwContext = context as ViewContext;
        var fieldId = vwContext.ViewData.TemplateInfo.GetFullHtmlFieldId(metadata.PropertyName);

        //...... Collapsed code
        yield return clientValidationRule;
    }
    //...... Collapsed code
}

GetFullHtmlFieldId的结果取决于我如何构建asp.net mvc页面:

The result of GetFullHtmlFieldId depends on how I build my asp.net mvc page:

// Edit.cshtml or Create.cshtml    
@Html.EditorFor(model => model.MyBoolProperty)
// Shared/EditorTemplates/Boolean.cshtml
@Html.CheckBoxFor(model => model)

结果GetFullHtmlFieldId 不正确:MyBoolProperty_MyBoolProperty

result of GetFullHtmlFieldId incorrect: MyBoolProperty_MyBoolProperty

// Edit.cshtml or Create.cshtml    
@Html.CheckBoxFor(model => model.MyBoolProperty)

GetFullHtmlFieldId 正确的结果:MyBoolProperty

result of GetFullHtmlFieldId correct: MyBoolProperty

即使使用更复杂的编辑器,我也会看到这种不正确的行为

Even with more complex editors I see this incorrect behavior

// Edit.cshtml or Create.cshtml  
@Html.EditorFor(model => model.JustAnArray[1].ComplexProperty.MyBooleanProperty)
// Shared/EditorTemplates/Boolean.cshtml
@Html.CheckBoxFor(model => model)

GetFullHtmlFieldId 不正确的结果:JustAnArray_1__ComplexProperty_MyBoolProperty_MyBoolProperty

result of GetFullHtmlFieldId incorrect: JustAnArray_1__ComplexProperty_MyBoolProperty_MyBoolProperty

// Edit.cshtml or Create.cshtml  
@Html.CheckBoxFor(model => model.JustAnArray[1].ComplexProperty.MyBooleanProperty)

GetFullHtmlFieldId 正确的结果:JustAnArray_1__ComplexProperty_MyBoolProperty

result of GetFullHtmlFieldId correct: JustAnArray_1__ComplexProperty_MyBoolProperty

这还会返回正确的值

// Edit.cshtml or Create.cshtml  
@Html.EditorFor(model => model.JustAnArray[1])
// Shared/EditorTemplates/ComplexProperty.cshtml
@Html.CheckBoxFor(model => model.MyBooleanProperty)

GetFullHtmlFieldId 正确的结果:JustAnArray_1__ComplexProperty_MyBoolProperty

result of GetFullHtmlFieldId correct: JustAnArray_1__ComplexProperty_MyBoolProperty

使用@Html.CheckBoxFor(model => model)看起来像,它给出的结果不正确,但是使用@Html.CheckBoxFor(model => model.MyBoolProperty)时,它可以按预期工作

It looks like that using @Html.CheckBoxFor(model => model), it gives incorrect results but when using @Html.CheckBoxFor(model => model.MyBoolProperty) it is working as expected

我在其他控件(例如TextBoxFor)上也遇到相同的问题

I have the same issue with other controls (like TextBoxFor)

我如何在我的验证属性中获取控件的正确fieldId,而与构建页面的方式无关.

How can I get the proper fieldId of my control in my validation attribute, independent of how you build the page.

我宁愿使用现有的方法(也许与TextBoxFor和CheckBoxFor以及其他控件所使用的方法相同),而不要使用模仿该功能.如果我模仿fieldId的生成,那么我所做的更改是我不会在ASP.NET控件可以处理的所有情况下解决.

I'd rather use already existing methods (maybe the same methods as which are used by TextBoxFor and CheckBoxFor and other controls) than mimic this already existing functionality. If I mimic the generation of the fieldId, I have a change I don't take care of all situations where the ASP.NET controls take care of.

推荐答案

似乎有效的前缀取决于用于构建页面的表达式,而GetClientValidationRules方法不可用.如果有人有解决方案,那将是很好的选择,但是当方法GetClientValidationRules试图初始化Java验证所需的数据时,您可以尝试在客户端解决此问题.

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.

在某些默认验证属性(如[Compare],这取决于第二个字段)上,它们设置参数*.propertyName,并在不引人注目的适配器中将*.部分替换为从输入中检索到的有效前缀名称属性.您可以尝试使用类似的方法获取ID.

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.

但是,如果您对另一个领域感兴趣,则需要这样做.在这种情况下,您似乎对要验证的同一输入字段的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", {});
});

希望有帮助!

这篇关于ASP.NET MVC GetFullHtmlFieldId不重设有效的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆