如何在ASP.Net视图中使用Html.RadioButtonFor?

时间:2012-02-13 01:38:13

标签: c# asp.net-mvc

我正在使用带有C#语言的ASP.Net MVC。 我想编辑一个用户。用户表有一个名为“IsActive”的布尔字段。 在模型中,我正在使用ADO.Net实体数据模型。

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) %>

在这种情况下,我想使用RadioButton而不是选项Yes和No. 怎么做? 谢谢!

2 个答案:

答案 0 :(得分:0)

lambda表达式的主体应该是IsActive属性的名称:

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

此外,您似乎想要checkbox而不是radio

答案 1 :(得分: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>
<%} %>