2011-10-09 101 views
1

我有,我已經使用的數據註解的類:如何將驗證消息添加到我的自定義HtmlHelper?

[Required(ErrorMessage = "You must indicate which sex you are.)] 
public string Sex { get; set; } 

我還創建了名爲RadioButtonListFor一個自定義的HtmlHelper,我可以這樣調用:

@Html.RadioButtonListFor(m => m.Sex, "SexList") 

我SexList這樣定義:

IList<string> SexList = new List() { "Male", "Female"}; 

而且下面是RadioButtonListFor擴展(沒有完全修好了):

public static class RadioButtonListForExtentions 
{ 
    public static IHtmlString RadioButtonListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, string list) 
    { 
     string prefix = ExpressionHelper.GetExpressionText(expression); 
     if (string.IsNullOrEmpty(prefix)) 
      prefix = "empty"; 
     int index = 0; 

     var items = helper.ViewData.Eval(list) as IEnumerable; 
     if (items == null) 
      throw new NullReferenceException("Cannot find " + list + "in view data"); 

     string txt = string.Empty; 
     foreach (var item in items) 
     { 
      string id = string.Format("{0}_{1}", prefix, index++).Replace('.','_'); 
      TagBuilder tag = new TagBuilder("input"); 
      tag.MergeAttribute("type", "radio"); 
      tag.MergeAttribute("name", prefix); 
      tag.MergeAttribute("id", id); 
      tag.MergeAttribute("data-val-required", "Missing"); 
      tag.MergeAttribute("data-val", "true"); 

      txt += tag.ToString(TagRenderMode.Normal); 
      txt += item; 
     } 

     return helper.Raw(txt); 
    } 
} 

我的問題是這樣的:現在我已經在屬性「data-val-required」中對單詞「Missing」進行了硬編碼。如何獲取我在數據註釋中陳述的文本?

回答

1

啊......找到了自己的解決方案後,良好的睡眠:-)

與下面的更換RadioButtonListFor:

public static IHtmlString RadioButtonListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, string list) 
    { 
     string prefix = ExpressionHelper.GetExpressionText(expression); 
     if (string.IsNullOrEmpty(prefix)) 
      prefix = "empty"; 
     int index = 0; 

     var items = helper.ViewData.Eval(list) as IEnumerable; 
     if (items == null) 
      throw new NullReferenceException("Cannot find " + list + "in view data"); 

     var validationAttributes = helper.GetUnobtrusiveValidationAttributes(prefix); 

     string txt = string.Empty; 
     foreach (var item in items) 
     { 
      string id = string.Format("{0}_{1}", prefix, index++).Replace('.','_'); 
      TagBuilder tag = new TagBuilder("input"); 
      tag.MergeAttribute("type", "radio"); 
      tag.MergeAttribute("name", prefix); 
      tag.MergeAttribute("id", id); 
      foreach (KeyValuePair<string, object> pair in validationAttributes) 
      { 
       tag.MergeAttribute(pair.Key, pair.Value.ToString()); 
      } 
      txt += tag.ToString(TagRenderMode.Normal); 
      txt += item; 
     } 

     return helper.Raw(txt); 
    } 

基本上我已經加入「validationAttributes」這顯然是一個我的驗證項目字典。循環瀏覽並添加它們使它像魅力一樣工作!

編輯2011年10月13日:

結束與下面的解決方案。我決定發送一個字典,其中的關鍵字是單選按鈕值,而字典的值是單選按鈕文本,而不是僅僅獲取字符串列表。

public static IHtmlString RadioButtonListFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, string list) 
    { 
     string prefix = ExpressionHelper.GetExpressionText(expression); 
     if (string.IsNullOrEmpty(prefix)) 
      prefix = "empty"; 

     // find existing value - if any 
     string value = helper.ViewData.Eval(prefix) as string; 

     var validationAttributes = helper.GetUnobtrusiveValidationAttributes(prefix); 
     string txt = string.Empty; 

     // create hidden field for error msg/value 
     TagBuilder tagHidden = new TagBuilder("input"); 
     tagHidden.MergeAttribute("type", "hidden"); 
     tagHidden.MergeAttribute("name", prefix); 
     tagHidden.MergeAttribute("value", value); 
     tagHidden.MergeAttribute("id", prefix.Replace('.', '_')); 
     foreach (KeyValuePair<string, object> pair in validationAttributes) 
     { 
      tagHidden.MergeAttribute(pair.Key, pair.Value.ToString()); 
     } 
     txt += tagHidden.ToString(TagRenderMode.Normal); 

     // prepare to loop through items 
     int index = 0; 
     var items = helper.ViewData.Eval(list) as IDictionary<string, string>; 
     if (items == null) 
      throw new NullReferenceException("Cannot find " + list + "in view data"); 

     // create a radiobutton for each item. "Items" is a dictionary where the key contains the radiobutton value and the value contains the Radiobutton text/label 
     foreach (var item in items) 
     { 
      string id = string.Format("{0}_{1}", prefix, index++).Replace('.','_'); 
      TagBuilder tag = new TagBuilder("input"); 
      tag.MergeAttribute("type", "radio"); 
      tag.MergeAttribute("name", prefix); 
      tag.MergeAttribute("id", id); 
      tag.MergeAttribute("value", item.Key); 
      if (item.Key == value) 
       tag.MergeAttribute("checked", "true"); 
      tag.MergeAttribute("onclick", "javascript:" + tagHidden.Attributes["id"] + ".value='" + item.Key + "'"); 
      txt += tag.ToString(TagRenderMode.Normal); 
      txt += item.Value; 
     } 

     return helper.Raw(txt); 
    }