我想這樣做,但這個只是沒有它旁邊的文本顯示單選按鈕..MVC和單選按鈕列表
<% foreach (string s in Html.RadioButtonList("rbl")) {%>
<% =s %>
<% } %>
我想這樣做,但這個只是沒有它旁邊的文本顯示單選按鈕..MVC和單選按鈕列表
<% foreach (string s in Html.RadioButtonList("rbl")) {%>
<% =s %>
<% } %>
如果是我,我只想用一系列靜態HTML元素。我知道有些人認爲做ASP這樣的倒退,但它簡化了國際海事組織,並最終形成了一個更可靠和可預期的[所以我編造了一個詞]圖形用戶界面。
查看codeplex上的MVC source中提供的MVC Futures DLL。其中有一個HtmlHelper擴展來渲染RadioButton列表。它可以自動在ViewData中渲染一個SelectList,或者你可以顯式地傳遞它。有幾種過載可用於不同的需求。
但它不會呈現標籤,因爲問題所述。 – usr 2010-01-13 14:20:08
它曾經在預覽中,但它被刪除。
如果你不能在期貨發現它嘗試這樣的事情
<% 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 />
<% } %>
Elijah Manor寫在ASP.NET MVC 1.0同樣的煩惱:
ASP.NET MVC Html.RadioButtonList Blues
他決定遍歷他的DataSource並創建單獨的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>
<% } %>
@{
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/>
}
看到漂亮的助手丹尼爾Gidman,2012年6月14日,here。 他爲MVC中的RadioButtonList創建了一個漂亮完美的助手。
使用這裏的radiolist助手:http://awesome.codeplex.com – Omu 2011-05-20 17:26:33