views:

97

answers:

1

I've two radio buttons,

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

How can I check which is selected, when the 1st is selected, I should be able to set the boolean value of 'rdPageType' to true, and the other as false, vice versa

Help please

A: 

Assuming you have the following model:

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

Your controller might look like this:

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);
    }
}

And the view:

<% 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" />
<% } %>
Darin Dimitrov
Thanks for the effort you put in Darin. I've a question. I have two radio buttons as said above, I have two values for instance,Radiobutton1 : ChocolateRadiobutton2 : MilkHere I am choosing 'chocolate'. I need to check in 'If' loop that which radio button I've selected and insert some data relevant to it to te DB.So for example, If I select 'chocolate' I should insert string 'chocolate' to a DB Table column, and if 'milk', thn milk to the column. Can you help me with the solution. Thanks a million
Sunil Ramu