• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2229
  • Last Modified:

Ensure radio button is checked in MVC view

I have an MVC view with three mutually exclusive radio buttons.   How can I ensure that at least one of them is selected when submitting to actionresult?

Here is code snippet of view below
<input name="Value1" type="radio" value="1"  />
<input name="Value2" type="radio" value="2"  />
<input name="Value1" type="radio" value="3"  />

Open in new window

0
ToString1
Asked:
ToString1
  • 6
  • 6
  • 2
8 Solutions
 
jamesbaileCommented:
If you use the Html helpers then you may find things easier.

Html.RadiobuttonFor
or

Html.RadioButton

therefore if I have a model like this

public enum RadioOption {FirstOption, SecondOption, ThirdOption}
public class MyModel
{
    public RadioOption SelectedOption {get; set;}
}

I can do the following in my view

<%= Html.RadioButtonFor(c => c.SelectedOption, RadioOption.FirstOption) %> Option 1
<%= Html.RadioButtonFor(c => c.SelectedOption, RadioOption.SecondOption) %> Option 2
<%= Html.RadioButtonFor(c => c.SelectedOption, RadioOption.ThirdOption) %> Option 3


0
 
ToString1Author Commented:
Thanks

It is a jquery implemementation of a radio button that has an image effect on it.

So I need the code

<input name="Value1" type="radio" value="1"  />
<input name="Value2" type="radio" value="2"  />
<input name="Value1" type="radio" value="3"  />

So really I want to handle in in my controller.

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SubmitForm(int ID, List<Foo> FOOs)
       {
           if FOOs.value1 == null
           {
              //HOW CAN I RETURN TO THE SAME VIEW WITH ERROR MESSAGE ?
           }

       

       }

0
 
ronan1979Commented:
set checkedbvalue:
<input name="Value1" type="radio" value="1" checked="checked" />
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
ToString1Author Commented:
Hi thanks but I need to validate the radio button
0
 
jamesbaileCommented:
Whether you are using JQuery or not should not effect the way you code your view.  You can still use <%= Html.RadioButtonFor(c => c.SelectedOption, RadioOption.FirstOption) %> along with Jquery.

I'd also get the controller to accept the model in the action

So

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SubmitForm(MyModel model)
{
    if (model.Value1 == null)
    {
        ViewData.ModelState.AddModelError("Value1","Value1 is null");
        return View("MyView",model);
    }

........

........
     RedirectToAction("MyModelUpdated");

}

0
 
ToString1Author Commented:
OK thanks

i am passing a strongly typed list of responses to the action result so in my view I have

        <% i = i + 1; %>        
        <input name="[<%=i%>].celue" type="radio" value="1"  />
        <input name="[<%=i%>].celue" type="radio" value="2"  />
        <input name="[<%=i%>].celue" type="radio" value="3"  />  

So how would I use HTML Helper in this case?


0
 
ronan1979Commented:
if you add checked="checked" to one input
then one of them will be set by default
(they need part of the same form/group)

        <% i = i + 1; %>        
        <input name="[<%=i%>].celue" type="radio" value="1"  checked="checked"  />
        <input name="[<%=i%>].celue" type="radio" value="2"  />
        <input name="[<%=i%>].celue" type="radio" value="3"  />  

(A) what do you mean by "validate the radio button" ?
(B) what do you mean by "HTML Helper" ?
0
 
jamesbaileCommented:
Can you explain is it that you have a group of three options you want the user to select from, but that there may 1 or more of these groups of options?
0
 
ToString1Author Commented:
Hi

Yes there are a group of options and I need to ensure ove option from each radio button set is clicked
0
 
ToString1Author Commented:
You can ignore the HTML Helper option.  Essentially if I have



        <% i = i + 1; %>        
        <input name="[<%=i%>].celue" type="radio" value="1"    />
        <input name="[<%=i%>].celue" type="radio" value="2"  />
        <input name="[<%=i%>].celue" type="radio" value="3"  />

I need to ensure one is checked.   I cannot force one to be checked when loading the view.

So in my controller post actionresult

I need a way of saying

public ActionResult SubmitForm(int ID, List<Foo> FOOs)
       {
         foreach thisFoo in FOO
           {
              if  foo == NULL then
              //HOW CAN I RETURN TO THE SAME VIEW WITH ERROR MESSAGE ?
           }

       

       }

0
 
jamesbaileCommented:
If you are using ASP.NET MVC 2 you can use the model validators

Create the following simple model

public enum RadioOption {FirstOption, SecondOption, ThirdOption}
public class MyModel
{
    [Required]
    public RadioOption? SelectedOption {get; set;}
}

and then an action which looks like

public ActionResult SubmitForm(int ID, List<MyModel> options)
{
  if (!ViewData.ModelState.IsValid)
{
}

0
 
jamesbaileCommented:
Sorry I saved the response before I'd finished writing

The action rfesult should look something like

public ActionResult SubmitForm(int ID, List<MyModel> options)
{
      if (!ViewData.ModelState.IsValid)
     {
            return View("MyView"); // you may wish to pass back the view model with the options set
     }
.......
     RedirectToAction("Complete");

}

0
 
jamesbaileCommented:
Also you will need to make sure that your radio buttons are named correctly in the view.

<input name="options[<%=i%>]" type="radio" value="1"    />

The name needs to match up with the variable/property name for the default model binder to bind properly.
0
 
ToString1Author Commented:
Thanks

Yes because I have a list of survey results that can vary between question so I have

 <% i = i + 1; %>        
        <input name="[<%=i%>].celue" type="radio" value="1"    />
        <input name="[<%=i%>].celue" type="radio" value="2"  />
        <input name="[<%=i%>].celue" type="radio" value="3"  />

But this could be one two or 10 radio buttons depending on the question.   Also my model binding works fine because I have a property "celue" in my model so when I have

1.celue
2.celue
3.celue

It persists to database no problem when I call actionresult

//post
public ActionResult SubmitForm(int ID, List<Foo> FOOs)

So could you explain again from here what I need to do.  

You see " Foo" is a table within linq to sql model so should I partial class that?   If I do could you give an example  please?

Thanks for your commets


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 6
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now