web-dev-qa-db-ja.com

MVCとRadioButtonList

これを試してみましたが、ラジオボタンの横にテキストが表示されないだけです。

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

Elijah Manor ASP.NET MVC1.0で同じ問題について書いています:

ASP.NET MVC Html.RadioButtonList Blues

彼は自分のデータソースをループして、個々のHtml.RadioButtonとLabelの組み合わせを作成することにしました。

<!-- 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>
<% } %>
12
Zack Peterson

以前はプレビューに含まれていましたが、削除されました。

あなたが将来それを見つけることができないならば、このような何かを試みてください

<% 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 />
<% } %>
8
ccook

私の場合は、一連の静的HTML要素を使用します。 ASP日間にこのようなスローバックを行うことを検討している人もいますが、IMOを簡素化し、信頼性と期待性を高める[Wordを作成しました] GUIを作成することになります。

7
brady gaster
@{
    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/>
}
6
charith

ダニエル・ギッドマン著、2012年6月14日、 ここ のニースヘルパーを参照してください。彼はMVCでRadioButtonListの素敵で完璧なヘルパーを作成しました。

0
A.Dara