Radiobuttonlist in MVC 5 using Model, Radiobuttonlistfor


In this post we are going to learn how to implement radiobuttonlist in mvc 5 with model using html helper radiobuttonlistfor

Model:

First add a model and a property called Gender as follows

public class GenderModel
    {
        public string Gender { get; set; }
    }

 

Controller:

Add a controller and a action method in it as follows

public ActionResult RdBtnList(GenderModel model)
        {
            return View(model);
        }

 

in this action method we are passing empty model to view in get call, and we will pass model with data in post call.

View:

@model MVCControls.Models.GenderModel
@{
    ViewBag.Title = "Radio Button List using Model || Chikkanti.Wordpress.com";
}

<h2>Radio Button List using Model</h2>
@using (Html.BeginForm())
{
    @Html.RadioButtonFor(g => g.Gender, "Male")@Html.Label("Male")
    @Html.RadioButtonFor(g => g.Gender, "Female")@Html.Label("Female")
    <br />
    <input type="submit" value="Submit" /><br />
    if (!string.IsNullOrEmpty(Model.Gender))
    {
        @Html.Label("Your selected option: " + Model.Gender);
    }
}

 

Now Rebuild your application and run the application.

image

image

image

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s