3

ラジオボタンを管理するための独自のカスタムhtmlhelperを開発しました。私のカスタムヘルパーから基本的なhtmlタグを生成するための問題はありません。しかし、私はHTMLタグ(クライアント側の控えめな検証)に検証属性を注入する際に問題があります。モデル(データ注釈)から検証属性を取得するためにhtmlHelper.GetUnobtrusiveValidationAttributes(接頭辞)を使用しましたが、カスタムRequiredAttributeに対しては機能しません。ここで私のカスタムhtmlヘルパーで(私のモデルから)バリデーション属性を取得できません

は、私の見解モデルの一部である:ここで

public class MaterialEditNewViewModel 
{ 
    public int RequestId { get; set; } 

    [CustomRequired] 
    Public bool ADR { get; set; } 
    ... 
} 

は私CustomRequiredです:ここでは

public class CustomRequiredAttribute : RequiredAttribute 
{   
    public override string FormatErrorMessage(string name) 
    { 
     string translatedFieldName = UserResource.ResourceManager.GetString(name); 
     if (string.IsNullOrWhiteSpace(translatedFieldName)) 
      translatedFieldName = name; 
     return string.Format(UserResource.FieldRequired, translatedFieldName); 
    } 
} 

は私のカスタムHTMLヘルパーです:

public static IHtmlString RadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, string labelText) 
    { 
     var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); 
     string prefix = ExpressionHelper.GetExpressionText(expression); 

     var validationAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(prefix); 

     object currentValue = metadata.Model; 
     string property = metadata.PropertyName; 

     // Build the radio button html tag 
     var htmlRadio = new TagBuilder("input"); 
     htmlRadio.GenerateId(property + value); 
     htmlRadio.Attributes["type"] = "radio"; 
     htmlRadio.Attributes["name"] = property; 
     htmlRadio.Attributes["value"] = Convert.ToString(value); 

     foreach (KeyValuePair<string, object> pair in validationAttributes) 
     { 
      htmlRadio.MergeAttribute(pair.Key, pair.Value.ToString()); 
     } 

     if (object.Equals(currentValue, value)) 
     { 
      htmlRadio.Attributes["checked"] = "checked"; 
     } 

     // Build the label html tag 
     var label = new TagBuilder("label"); 
     label.Attributes["for"] = htmlRadio.Attributes["id"]; 
     label.SetInnerText(labelText); 

     // Return the concatenation of both tags 
     return new HtmlString(htmlRadio.ToString(TagRenderMode.SelfClosing) + label.ToString() 
     ); 
    } 
  1. あなたは私のCustomRequiredデータアノテーションnは@ Html.RadioButtonFor(model => model.ADR)のような基本ヘルパーで動作しますが、カスタムhtmlhelpersで使用すると動作しません。

  2. 私は、私のモデルで[必須]のような '古典的な'データ注釈を使用すると、検証属性を取得するのに問題はありませんが、私のCustomRequiredAttributeを使用するとGetUnobtrusiveValidationAttributesは何も返しません。

私が明確でない場合は、私に明確にすることを躊躇しないでください。

MVC3でEntity Framework Code Firstを使用していると忘れてしまいました。

ありがとうございました。

答えて

0

RequiredAttributeIClientValidatableインターフェイスを実装していないため、カスタム属性用のカスタムアダプタを登録する必要があります。あなたはApplication_Startに以下の行を追加することでこれを行うことができます:

DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof(CustomRequiredAttribute), 
    typeof(RequiredAttributeAdapter) 
); 

クライアント側の検証属性を実装するための2つの方法があります。

  • は、カスタム検証がIClientValidatableインタフェース
  • Aレジスタを実装する属性を持ちますカスタムアダプタ

RequiredAttributeは2番目の方法を使用します。これは、すべての必要な属性にRequiredAttributeAdapterを関連付けます。しかし、あなたはRequiredAttributeから派生しているので、カスタム属性にはアダプタがなくなります。したがって、Application_Startに登録する必要があります。

+0

ありがとう、それはかなりうまくいきます。ちょうど1つの質問:私は自分のビューで2つの他のフィールドに私のCustomRequiredを使用しますが、カスタムヘルパーではなく、Html.TextBoxForを使用して、これらの要素のためにクライアント側の検証を行うために私のカスタムアダプタを登録する必要はありませんでした。説明できますか? – Bronzato

関連する問題