2012-10-16 3 views
0

MVC 면도기 프로젝트에서 RadioButtonList를 사용하여 라디오 버튼을 생성합니다. 내 라디오 버튼 목록이 미리 선택된 값을 가져옵니다 몇 가지 이유를 들어미리 선택된 값이없는 MVC 면도기 RadioButtonList

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

: 은 여기 내 코드의 예입니다. 첫 번째 체크 박스가 항상 선택되어있어 UI가 다소 혼란 스럽습니다.

어떻게하면 좋습니까?

한 가지 방법은 전체 페이지를 Jquery로 반복하고 각 상자의 선택을 취소하는 것입니다. 그러나 그것은 imho 주위에 꽤 작동하지 않습니다.

편집 : 다음은 사용자 정의 열거 형 인 HouseType에 대한 자세한 정보입니다.

public enum HouseType 
{ 
    House, 
    Apartment, 
    Garage 
}; 

하고이 라인

public HouseType HouseType { get; set; } 

답변

1

당신은 HouseType 재산 뷰 모델에 nullable 형식 만들 수를 사용하여 부름. 예를 들어이 열거 형의 경우 :

public HouseTypes? HouseType { get; set; } 

하거나 정수의 경우 :

public int? HouseType { get; set; } 

업데이트 : 당신이 following helper를 사용하고 있는지

보인다. 이 도우미는 nullable enum 값을 지원하지 않습니다.

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 허용 열거와 먹힐을하고 해당 속성의 값이 null의 경우는 라디오 버튼을 선택하지 않습니다 그래서 적응.

+0

HouseType은 enum입니다. 자세한 정보는 편집을 참조하십시오. 이 nullable을 어떻게 만들 수 있습니까? "Public HouseType HouseType {get; set;}"에 물음표를 추가하면 RadioButtonListHelper 클래스가 "객체 참조가 객체 인스턴스로 설정되지 않았기 때문에"lostOfValues를 통해 루프하려고하면 충돌이 발생합니다. – Blizwire

+0

이 경우에는'RadioButtonListHelper'를 보여 주어야합니다. 그것은 MVC의 일부인 비표준 보조자입니다. 네가 어딘가에서 가져간 것 같아. –

+0

여기에 : http://pastebin.com/gT52mqZQ – Blizwire