2012-02-13 36 views
0

我正在使用C#語言的ASP.Net MVC。 我想編輯一個用戶。用戶表具有名爲「IsActive」的布爾字段。 在模型中,我使用了ADO.Net實體數據模型。如何在ASP.Net視圖中使用Html.RadioButtonFor?

public ActionResult Edit(int id) { 
    var editUser = (from u in userEntity.USERs where u.UserId == id select u).First(); 
    return View(editUser); 
} 

在視圖中,由Visual Studio for IsActive生成的腳本使用TextBox。

<%= Html.TextBoxFor(model => model.Activate) %> 

在這種情況下,我想用單選按鈕,而不是用的選項是和 號怎麼辦呢? 謝謝!

回答

0

的lambda表達式的身體應該是IsActive屬性的名稱:

<%= Html.CheckBoxFor(model => model.IsActive) %>

而且,好像你想要一個checkbox而不是radio

+0

感謝您的回覆! 但在這裏我想使用單選按鈕。怎麼樣? – 2012-02-13 01:44:12

+0

當我使用它時,它顯示錯誤:CS0266:不能隱式轉換類型'bool?'到'布爾'。存在明確的轉換(您是否缺少演員?) – 2012-02-13 02:02:01

+0

然後將其更改爲'model.IsActive.Value' - 它是'可空的'。這意味着數據庫中的'IsActive'字段允許'null',因此實體框架將該類型映射到'可爲空'' – xandercoded 2012-02-13 02:03:19

0
<%if(Model.Activate){ %> 
    <%: Html.RadioButtonFor(model => model.Activate, true, new { @id = "radio1", @name = "Activate", @checked="checked" })%>    
    <label for="radio1">Yes</label> 
    <%: Html.RadioButtonFor(model => model.Activate, false, new { @id = "radio2", @name = "Activate" })%> 
    <label for="radio2">No</label> 
    <%}else { %> 
    <%: Html.RadioButtonFor(model => model.Activate, true, new { @id = "radio1", @name = "Activate" })%>    
    <label for="radio1">Yes</label> 
    <%: Html.RadioButtonFor(model => model.Activate, false, new { @id = "radio2", @name = "Activate", @checked = "checked" })%> 
    <label for="radio2">No</label> 
<%} %> 
相關問題