Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Razor Radio Button

In partial view I work with textboxes like this.

@model Dictionary<string, string> @Html.TextBox("XYZ", @Model["XYZ"]) 

How can i generate radiobuttons, and get the desired value in the form collection as YES/NO True/False) ? Currently i am getting null for "ABC" if i select any value for the below.

   <label>@Html.RadioButton("ABC", @Model["ABC"])Yes</label>    <label>@Html.RadioButton("ABC", @Model["ABC"])No</label> 

Controller

        public int Create(int Id, Dictionary<string, string> formValues)         {          //Something Something         } 
like image 943
Nanu Avatar asked May 29 '12 19:05

Nanu


People also ask

How to use radio button in MVC c#?

RadioButton is a kind of toggle control which receives the input from a user in the form of a click. In radio-button, all buttons are connected with the same group name and id. User can select any one button option from the given radiobuttons or list.

How to use radio button for Gender in MVC?

The second parameter is the value of the radio button which will be sent to the server when the respective radio button is checked. That means if the Male radio button is selected, then the string value “Male” will be assigned to a model property with the name Gender and submitted to the server.

What is radiobutton in MVC?

RadioButton is a kind of toggle control which receives the input from a user in the form of a click. In radio-button, all buttons are connected with the same group name and id. User can select any one button option from the given radiobuttons or list. In ASP.NET MVC, there are three ways to create a RadioButton...

How do I use a radio button in a razor page?

Working With Radio Buttons in ASP.NET Razor Pages. The radio button control is designed to support the selection of only one of a mutually exclusive set of predefined options. The radio control is rendered in HTML by setting the type attribute in an input element to radio: <input type="radio" />.

Can I use HTML helper radio-button in model?

This HTML Helper radio-button is not attached / bound with any Model. You can directly use HTML element of a Radio button. Just follow the below images to see how to work around these.

What is the use of radiobutton?

RadioButton is a kind of toggle control which receives the input from a user in the form of a click. In radio-button, all buttons are connected with the same group name and id. User can select any one button option from the given radiobuttons or list.


1 Answers

In order to do this for multiple items do something like:

foreach (var item in Model) {     @Html.RadioButtonFor(m => m.item, "Yes") @:Yes     @Html.RadioButtonFor(m => m.item, "No") @:No } 
like image 137
mattytommo Avatar answered Oct 12 '22 22:10

mattytommo