Friday, 6 December 2013

Genarating Radiobuttonlist Control in MVC 4

Here i am going to explain generate a radiobuttonlist in mvc. In previous articles i am explained code first approach,before seeing this article please see that article,you will get better idea on how to enable migrations,and how to craete data context class.click here to go that article. For this Create Two Model Classes one is Department and another one is Company.

 public class Department
    {
        [Key]
        public int DeptId { get; set; }
        public string DeptName { get; set; }
    }


Create company Model class and write the following code

public class Company
    {
        public string SelectDepartment { get; set; }

        public List<Department> departments
        {
            get
            {
                DataContext db = new DataContext();
                return db.department.ToList();
            }
        }

    }


In above code DataContext class is in Models folder..(to know about datacontext class and furthur details click here)

datacontext class
public class DataContext:DbContext
    {
        public DataContext() : base("EmployeeDb")
        {
        }
        public DbSet<Employee> employee { get; set; }

        public DbSet<Department> department { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            base.OnModelCreating(modelBuilder);
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<DataContext, Configuration>());
            modelBuilder.Entity<Employee>().ToTable("employee");
            modelBuilder.Entity<Department>().ToTable("department");
     }

 we have to include department class in datacontext class..if we change model class the database table will be changed for that we have to write onmodelcreating method.




Now go to Home controller and write one action method name it as Radio and write the following code

 public ActionResult Radio()
        {
            Company comapny = new Company();
            return View(comapny);
        }


right click on radio and add view to action method



now write radiobutton control in form and write the following code

@model MvcEmployee.Models.Company

@{
    ViewBag.Title = "Radio";
}

<h2>Radio</h2>
@using (Html.BeginForm())
{
    foreach(var dept in Model.departments)
    {
       @Html.RadioButtonFor(m=>m.SelectDepartment,dept.DeptId)@dept.DeptName
  

  <br />
 }   
     
    <input type="submit" value="Submit" />
}







Now go to Home Controller write the HttpPost method for Radio Action method

  [HttpPost]
        public string  Radio(Company company)
        {
            if (string.IsNullOrEmpty(company.SelectDepartment))
            {
                return "You did not select any department";
            }
            else
                return "You selected department with Id " + company.SelectDepartment;

        }






Now run the application and you see the following output









 If you didn't select any department and click on submit it shows "You did not select any department".
If you select Finance department and click on submit  it shows "You selected department with Id 3"


tags:radiobuttonlist in mvc 3,radiobuttonlist in mvc ,radiobuttonlist in mvc 4,radiobuttonlist in mvc 5,generate radiobuttonlist in mvc,radiobuttonlist and getting the values from data base,how to create radiobutton in mvc 4 razor,radiobutton in mvc razor,radiobutton list asp.net mvc 4

No comments:

Post a Comment