Translate

Thursday 26 September 2013

Multiple Radio Button List in Asp.Net MVC3 or MVC4 Using Enum Or Muliple Radio Button List Using Selected List Iems

Multiple Radio Button List in Asp.Net MVC3 or MVC4 Using Enum

Hi Developers,

I going to show the Mutiple RadioButton List MVC3 using  Enum .How To show and save the particular radio button value in data base.



Model Property :

public BowlerType? BowlerActive { get; set; }

public enum BowlerType
    {
        FastMedium,
        LegSpinner,
        MediumPace,
        OffSpinner
    }



 Employeee Controller :

Here I’m creating a list of items for  Multiple radio button list and returning.
private List<SelectListItem> BowlerActive()
        {
            var opt = new List<SelectListItem>();
            opt.Add(new SelectListItem { Value = "Fast Medium", Text = "Fast Medium" });
            opt.Add(new SelectListItem { Value = "Leg Spinner", Text = "Leg Spinner" });
            opt.Add(new SelectListItem { Value = "Medium Pace", Text = "Medium Pace" });
            opt.Add(new SelectListItem { Value = "Off Spinner", Text = "Off Spinner" });
            return opt;
        }



Suppose if want to create a some data regarding players type details. In Controller directly u can assign into a some ViewData or ViewBag or Session. so here I’m showing using ViewData how to bind into particular list of items.


So now I’m going to show the view how to retrieve the Radio Button gender list.
View  :

@Html.RadioButtonFor(x => x.BowlerActive, "FastMedium")
@Html.RadioButtonFor(x => x.BowlerActive, "LegSpinner")
@Html.RadioButtonFor(x => x.BowlerActive, "MediumPace")
@Html.RadioButtonFor(x => x.BowlerActive, "OffSpinner")



Or Else

ViewData["BowlerActiveList "] = new SelectList(BowlerActive (), "Value", "Text");



@foreach (var item in (IEnumerable<SelectListItem>)ViewData["BowlerActiveList"])
                           {
                           <ul>
                           @Html.RadioButtonFor(model => model.BowlerActive, new { @item.Value, @item.Text })
                           <label for = @item.Value> @item.Text</label>
                           </ul>
                           }



Note :  when you are saving the Enum Type, before we need to convert into Enum parse. Ex :    BowlerActive = (BowlerType)Enum.Parse(typeof(BowlerType),str),


 Happy Coding...

  Jagan Mohan




No comments:

Post a Comment