HI WELCOME TO SIRIS

Part 37 - Generating a radiobuttonlist control in mvc using HTML helpers

Leave a Comment
we will discuss, generating a radiobuttonlist in mvc using Html.RadioButtonFor helper. 

Right click on the "Models" folder and add a class file with "name=Company.cs". Copy and paste the following code.
public class Company
{
    public string SelectedDepartment { get; set; }
    public List<Department> Departments
    {
        get
        {
            SampleDBContext db = new SampleDBContext();
            return db.Departments.ToList();
        }
    }
}

Copy and paste the following 2 "Index" action methods in HomeController class.
[HttpGet]
public ActionResult Index()
{
    Company company = new Company();
    return View(company);
}

[HttpPost]
public string Index(Company company)
{
    if (string.IsNullOrEmpty(company.SelectedDepartment))
    {
        return "You did not select any department";
    }
    else
    {
        return "You selected department with ID = " + company.SelectedDepartment;
    }
}

Right click on the "Index" action method in "HomeController" and add a view with "name=Index". Copy and paste the following code. 
@model MVCDemo.Models.Company
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm())
{
    foreach (var department in Model.Departments)
    {
        @Html.RadioButtonFor(m => m.SelectedDepartment, department.Id) @department.Name
    }
    <br />
    <br />
    <input type="submit" value="Submit" />
}

Run the application and click on "Submit" without selecting any department. Notice that, you get a message stating you have not selected any department. On the other hand, select a department and click "Submit". The selected department ID must be displayed.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.