Monday, March 16, 2015

MVC : How to use RadioButtonFor


Following is the way to use Radio Button List in MVC:
1.       Create an ENUM for radio buttons list:
a.  public enum StatusList {
b.  Active = 0,
c.  Inactive = 1
d.      }
2.       Include ENUM in Model class:
a.       public StatusList Status { get; set; }
3.  Set Enum value in Controller:
a.  int val = 0;
b.      matchItem.Status = (StatusList)val;
4.  Use ENUM  in view:
a.  <div>@Html.RadioButtonFor(p => p.Status, Core.enums.StatusList.Active, new { onClick = "this.form.submit();" }) <strong>Show only Active</strong></div>
b.  <div>@Html.RadioButtonFor(p => p.Status, Core.enums.StatusList.Inactive, new { onClick = "this.form.submit();" }) <strong>Show only Inactive </strong></div>
5.       Get Enum value in Controller:

a.       int Status = (int)collection.Status;