Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 

This blog is part of 1 Tute Sets.

MVC
  • How to bind RadioButton using Enum in Asp.Net MVC

    • 1/1
    • 1
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 9.20k
    Comment on it

    How to bind RadioButton using Enum in Asp.Net MVC

    For binding RadioButton using Enum in Asp.Net MVC, we have to follow the following steps:-

    Step1: First create the Model class say 'RegisterationModel' in the Models Folder for the RadioButtons.

    Example:

    public class RegisterationModel
        { 
    
            public RadioOptions category { get; set; }         
    
            public enum RadioOptions
              {
                 General = 1,
                 SC = 2,
                 ST = 3,
                 OBC = 4
              }
      }
    

    Note=> public RadioOptions category { get; set; } will hold the value of the selected RadioButton.

    Step 2: Now in the View i.e. .cshtml File bind these enum values to the RadioButtons.

    Example:

    @Html.RadioButtonFor(model => model.category, Training_Task.Models.RegisterationModel.RadioOptions.General) 
    @Html.RadioButtonFor(model => model.category, Training_Task.Models.RegisterationModel.RadioOptions.SC) 
    @Html.RadioButtonFor(model => model.category, Training_Task.Models.RegisterationModel.RadioOptions.ST) 
    @Html.RadioButtonFor(model => model.category, Training_Task.Models.RegisterationModel.RadioOptions.OBC) 
    

    This will create the RadioButtons for entries in the enum RadioOptions. But to create the label field with each RadioButton write the following code.

    @Html.RadioButtonFor(model => model.category, Training_Task.Models.RegisterationModel.RadioOptions.General) @Html.Label("General")
    @Html.RadioButtonFor(model => model.category, Training_Task.Models.RegisterationModel.RadioOptions.SC) @Html.Label("SC")
    @Html.RadioButtonFor(model => model.category, Training_Task.Models.RegisterationModel.RadioOptions.ST) @Html.Label("ST")
    @Html.RadioButtonFor(model => model.category, Training_Task.Models.RegisterationModel.RadioOptions.OBC) @Html.Label("OBC")
    

    This will create the RadioButtons along with the label corresponding to each RadioButton. The value of the selected RadioButton can be retrieved from the category variable in the Controller.

    The RadioButtons will look like:

    alt text

    Hope it helps...!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: