2010-02-25 182 views
0

在一個asp.net mvc表單中,使用單選按鈕集來設置一個屬性。asp.net mvc:使用html.radiobutton的最佳方式

<%=Html.RadioButton("Tipo", "Pizza", 
    CType(Model.Tipo = "Pizza", Boolean), New With {.id = "Pizza"})%> 
    <label for="Pizza">Tipo Pizza</label> 

<%=Html.RadioButton("Tipo", "Barra", 
    CType(Model.Tipo = "Barra", Boolean), New With {.id = "Barra"})%> 
    <label for="Barra">Tipo Barra</label> 

我需要CType或我得到一個超載錯誤。

這種情況看起來像使用Model屬性時最常用的單選按鈕。

當然,我可以創建一個局部視圖或控件,但除此之外,是否有更清晰的代碼來完成此操作?

回答

1

我不知道爲什麼你使用CTYPE ......爲什麼不

<%=Html.RadioButton("Tipo", "Barra", 
    Model.Tipo == "Barra", 
    new { id = "Barra" })%> 
<label for="Barra">Tipo Barra</label> 

<%=Html.RadioButton("Tipo", "Barra", 
    Model.Tipo.Equals("Barra"), 
    new { id = "Barra" })%> 
<label for="Barra">Tipo Barra</label> 
相關問題