2012-10-16 18 views
0

Im在MVC Razor項目中使用RadioButtonList生成我的單選按鈕。 這裏是我的代碼示例:沒有任何預選值的MVC Razor RadioButtonList

@Html.RadioButtonList(n => n.HouseType) 

出於某種原因,我的單選按鈕,列表獲取預選值。第一個複選框總是被選中,這使得我的UI有點混亂。

如何禁用這個好方法?

一種方法是用Jquery遍歷整個頁面並取消選擇每個框。但這不是一個漂亮的工作周圍imho。

編輯: 下面是關於HouseType的更多信息,這是一個自定義的枚舉。

public enum HouseType 
{ 
    House, 
    Apartment, 
    Garage 
}; 

及其使用這條線

public HouseType HouseType { get; set; } 

回答

1

你可以使HouseType屬性可空類型上的視圖模型呼籲。例如,如果它是一個枚舉類型:

public HouseTypes? HouseType { get; set; } 

,或者如果它是一個整數:

public int? HouseType { get; set; } 

UPDATE:

看來你正在使用的following helper。此幫助程序不支持可枚舉枚舉值。因此,適應它:

public static class RaidioButtonListHelper 
{ 
    /// <summary> 
    /// Create a radiobutton list from viewmodel. 
    /// </summary> 
    public static MvcHtmlString RadioButtonList<TModel, TResult>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TResult>> expression, IEnumerable<SelectListItem> listOfValues = null) 
    { 
     var typeOfProperty = expression.ReturnType; 

     // Added by Darin Dimitrov to support nullable enums 
     var underlyingType = Nullable.GetUnderlyingType(typeOfProperty); 
     if (underlyingType != null) 
     { 
      typeOfProperty = underlyingType; 
     } 
     // End of addition 

     if (listOfValues == null && typeOfProperty.IsEnum) 
     { 
      listOfValues = new SelectList(Enum.GetValues(typeOfProperty)); 
     } 

     var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); 

     // Ctreat table 
     TagBuilder tableTag = new TagBuilder("table"); 
     tableTag.AddCssClass("radio-main"); 

     // Create tr:s 
     var trTagLable = new TagBuilder("tr id=\"" + metaData.PropertyName + "Lables\""); 
     var trTagRadio = new TagBuilder("tr id=\"" + metaData.PropertyName + "Radios\""); 

     foreach (SelectListItem item in listOfValues) 
     { 
      var text = item.Text; 
      var value = item.Value ?? text; 

      // Generate an id to be given to the radio button field 
      var id = string.Format("{0}_{1}", metaData.PropertyName, value); 

      // Create the radiobuttons 
      var radioTag = htmlHelper.RadioButtonFor(expression, value, new { id = id }).ToHtmlString(); 

      // Create the label for the radiobuttons. 
      var labelTag = htmlHelper.Label(id, HttpUtility.HtmlEncode(text)); 

      // Add the lables and reaiobuttons to td:s 
      var tdTagLable = new TagBuilder("td style=\"padding-left: 10px; text-align: center\""); 
      var tdTagRadio = new TagBuilder("td style=\"padding-left: 10px; text-align: center\""); 
      tdTagLable.InnerHtml = labelTag.ToString(); 
      tdTagRadio.InnerHtml = radioTag.ToString(); 

      // Add tds: to tr:s 
      trTagLable.InnerHtml += tdTagLable.ToString(); 
      trTagRadio.InnerHtml += tdTagRadio.ToString(); 

     } 

     // Add tr:s to table 
     tableTag.InnerHtml = trTagLable.ToString() + trTagRadio.ToString(); 

     //Return the table tag 
     return new MvcHtmlString(tableTag.ToString()); 
    } 
} 

現在是有一個可空枚舉要去工作,它不會選擇任何單選按鈕,如果相應屬性的值爲null。

+0

HouseType是一個枚舉。請參閱編輯瞭解更多信息。我如何使這個可以爲空?如果我向「public HouseType HouseType {get; set;}」添加問號,則RadioButtonListHelper類在嘗試循環lostOfValues時會崩潰,因爲「對象引用未設置爲對象的實例」。 – Blizwire

+0

那麼在這種情況下,你將不得不顯示這個'RadioButtonListHelper'。這是一個非標準的幫手,是MVC的一部分。我想你是從某個地方拿走它的。 –

+0

它在這裏:http://pastebin.com/gT52mqZQ – Blizwire

相關問題