2015-05-01 21 views
1

我提供額外的過載RadioButtonFor並希望將鍵值對添加到在傳遞的HTML屬性。我如何轉換匿名對象HTML屬性的詞典<字符串,對象>

由於

new { id = "someID" } 

當我使用的好像是我發現的建議),其產生與4項與鍵的字典中HtmlHelper.AnonymousObjectToHtmlAttributes法「的Comparer:我傳遞類似的例子「,」計數「,」鍵「,」值「。然後我嘗試使用Reflection遍歷「Keys」和「Values」中的值,但無法使其工作。

基本上我想要做的就是能夠將htmlAttributes強制轉換爲IDictionary,添加一個項目並將其傳遞給常規的RadioButtonFor方法。

編輯: 繼承人我真的想要做什麼。提供一個稱爲isDisabled的重載,以便能夠設置單選按鈕的禁用狀態,因爲無法直接使用HTML屬性直接完成此操作,因爲disabled = false stillr esults會禁用正在呈現以標記和禁用收音機。

public static MvcHtmlString RadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, bool isDisabled, object htmlAttributes) 
    { 
     var linkAttributes = System.Web.Mvc.HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); 
     Dictionary<string, object> htmlAttributesDictionary = new Dictionary<string, object>(); 
     foreach (var a in linkAttributes) 
     { 
      if (a.Key.ToLower() != "disabled") 
      { 
       htmlAttributesDictionary.Add(a.Key, a.Value); 
      } 
     } 

     if (isDisabled) 
     { 
      htmlAttributesDictionary.Add("disabled", "disabled"); 
     } 

     return InputExtensions.RadioButtonFor<TModel, TProperty>(htmlHelper, expression, value, htmlAttributesDictionary); 
    } 
+1

'RadioButtonFor()'已經有一個重載的html屬性。你究竟在做什麼?顯示你的方法的代碼。 –

+0

更新的問題與實際代碼 –

回答

4

看起來你可能會被應用AnonymousObjectToHtmlAttributes或者兩次或錯誤的項目。

沒有更多的代碼,就很難說

var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(new { id = "someID" }); 
attributes.Count = 1 
attributes.Keys.First() = id 

var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(new { id = "someID" })); 
attributes.Count = 3 
attributes.Keys.Join = Count,Keys,Values 

相比,在寫你的過載,請確保您的參數是:object htmlAttributesnew { }部分與過載IDictionary,例如:

Public static MvcHtmlString MyRadioButtonFor(..., object htmlAttributes) 
{ 
    return MyRadioButtonFor(...., HtmlHelper.AnonymousObjectToHtmlAttrbites(htmlAttributes); 
} 

public static MvcHtmlString MyRadioButtonFor(..., IDictionary<string, object> htmlAttributes) 
{ 
    htmlAttributes.Add("item", item); 
    return RadioButtonFor(..., htmlAttributes); 
} 

(僅僅是明確的,千萬不要用我的... - 它只是爲了舉例說明)

+0

更新的問題與實際代碼exmaple –

+0

謝謝,工作!在視圖中爲 –

0

它目前還不清楚爲什麼你不直接使用和接受object htmlAttributes現有超載添加disabled="disabled"屬性,但是下面應該工作

public static MvcHtmlString RadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, bool isDisabled, object htmlAttributes) 
{ 
    IDictionary<string, object> attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); 
    if (isDisabled && !attributes.ContainsKey("disabled")) 
    { 
     attributes.Add("disabled", "disabled"); 
    } 
    return InputExtensions.RadioButtonFor<TModel, TProperty>(htmlHelper, expression, value, attributes); 
} 
+0

var isDisabled = false; Html.RadioButtonFor(model => model.IsAircraftAndNotAsset,「true」,new {id =「rdoIsAircraftAndNotAssetYes」,disabled = isDisabled})仍會導致禁用的Radio。 –

+0

當然它是 - disabled =「disabled」和'disabled =「true」'和'disabled =「false」'和'disabled =「anything」'all設置禁用屬性(即使除了第一個都是無效的HTML)。這就是爲什麼我問我在第一個評論中你真的想做什麼。 –

+0

我試圖設置基於模型屬性的單選按鈕的禁用狀態。 –

相關問題