如何获取asp.net mvc Html.radiobuttonfor的选定值

时间:2010-07-22 13:47:36

标签: c# .net asp.net-mvc

我有两个单选按钮,

<%= Html.RadioButtonFor(m =>m.Type,true,new {id = "rdPageType",CHECKED = "checked" }) %>
<%= Html.RadioButtonFor(m =>m.Type,true,new {id = "rdPageType12",CHECKED = "checked" }) %>

如何选中哪一个,当选择第一个时,我应该能够将'rdPageType'的布尔值设置为true,另一个设置为false,反之亦然

请帮助

1 个答案:

答案 0 :(得分:1)

假设您有以下型号:

public class MyModel
{
    public bool RdPageType { get; set; }
}

您的控制器可能如下所示:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        // Action to render the form, initialize the model
        // with some default value
        var model = new MyModel
        {
            RdPageType = true
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyModel model)
    {
        // Action called when the form is posted
        // model.RdPageType will depend on the radio
        // being selected
        return View(model);
    }
}

观点:

<% using (Html.BeginForm()) { %>
    <%= Html.RadioButtonFor(m => m.RdPageType, true, new {id = "rdPageType" }) %>
    <%= Html.RadioButtonFor(m => m.RdPageType, false, new {id = "rdPageType12" }) %>    
    <input type="submit" value="OK" />
<% } %>