2008-12-05 13 views
19

tôi đã cố gắng làm điều này nhưng điều này chỉ hiển thị các radiobutton mà không cần văn bản bên cạnh nó ..MVC & RadioButtonList

<% foreach (string s in Html.RadioButtonList("rbl")) {%> 
    <% =s %> 
<% } %> 
+0

sử dụng trình trợ giúp radiolist tại đây: http://awesome.codeplex.com – Omu

Trả lời

7

Nếu là tôi, tôi sẽ chỉ sử dụng một loạt các yếu tố HTML tĩnh. Tôi biết một số xem xét làm một sự trở lại như vậy cho những ngày ASP, nhưng nó đơn giản hóa mọi thứ IMO và kết thúc làm cho một đáng tin cậy hơn và mong đợi [vì vậy tôi đã tạo ra một từ] GUI.

-1

Kiểm tra MVC Futures DLL có sẵn trong MVC source trên codeplex. Trong đó có một phần mở rộng HtmlHelper để hiển thị danh sách RadioButton. Nó có thể tự động hiển thị một SelectList trong ViewData hoặc bạn có thể truyền nó một cách rõ ràng. Một số tình trạng quá tải có sẵn cho các nhu cầu khác nhau.

+1

Nhưng nó không hiển thị nhãn, như câu hỏi đã nêu. – usr

8

Đã từng có trong bản xem trước nhưng nó đã bị xóa.

Nếu bạn không thể tìm thấy nó trong tương lai thử một cái gì đó như thế này

<% foreach (Model model in Models)) 
    { 
%><%= String.Format("<input type=\"radio\" value=\"{0}\" name=\"{1}\" id=\"{2}\"><label for=\"{2}\">{3}</label>", 
     model.ID, "fieldName", model.modelID, model.Name) %><br /> 
<% } %> 
12

Elijah Manor viết về những rắc rối tương tự trong ASP.NET MVC 1.0:

ASP.NET MVC Html.RadioButtonList Blues

Ông quyết định lặp qua DataSource của mình và tạo các kết hợp Html.RadioButton và Label riêng lẻ.

<!-- After using and looking at the code for the Html.RadioButtonList in the ASP.NET MVC 1.0 RTM codebase, I'm not sure how it is supposed to be useful. It only outputs the actual input radio button and doesn't render any corresponding labels. To get around this I ended up writing a foreach creating individual Html.RadioButton and labels --> 
<% 
var radioButtonList = new SelectList(new List<ListItem> { 
    new ListItem { Text = "Current", Value="false", Selected=true }, 
    new ListItem { Text = "Other", Value="true"}}, "Value", "Text", "false"); 
var htmlAttributes = new Dictionary<string, object> { 
    { "class", "radioButtonList" }, 
    { "onclick", "if(eval(this.value)) { $('#tblDate').show('slow'); } else { $('#tblDate').hide('slow'); }" } 
}; 
foreach (var radiobutton in radioButtonList) { %> 
    <%=Html.RadioButton("rblDate", radiobutton.Value, radiobutton.Selected, htmlAttributes)%> 
    <label><%=radiobutton.Text%></label> 
<% } %> 
6
@{ 
    var radioButtonList = new SelectList(new List<ListItem> { 
    new ListItem { Text = "1", Value="true", Selected=true }, 
    new ListItem { Text = "2", Value="false"}, 
    new ListItem { Text = "3", Value="false"}, 
    new ListItem { Text = "4", Value="false"}, 

    }, "Value", "Text", "false"); 

    var htmlAttributes = new Dictionary<string, object> { 
    { "class", "radioButtonList" }, 
    { "onclick", "if(eval(this.value)) { $('#tblDate').show('slow'); } else { $('#tblDate').hide('slow'); }" } 
};  
      } 

@foreach (var radiobutton in radioButtonList) { 

    @Html.RadioButtonFor(m => m.ContactDepartment, @radiobutton.Text) @radiobutton.Text 

    <br/> 
} 
0

Xem helper đẹp By Daniel Gidman, 14 tháng 6 năm 2012, here. Ông đã tạo ra một trợ giúp tốt đẹp và hoàn hảo cho RadioButtonList trong MVC.

Các vấn đề liên quan