Group of Radio Buttons - Asp.Net MVC

Feb 21, 2019 BY xtreme

introduction

HtmlHelper class includes two extension methods to generate Radio element in a razor view: RadioButton() and RadioButtonFor()

RADIOBUTTON

The Html.RadioButton() method creates a radio button element with a specified name, isChecked boolean and HTML attributes.

RADIOBUTTONFOR

The RadioButtonFor helper method is a strongly typed extension method. It generates radio element for the property specified using a lambda expression. RadioButtonFor method binds a specified model object property to RadioButtoncontrol. So it automatically checked or unchecked a RadioButton based on the property value.


RADIOBUTTONFOR - USING INTEGER

public class DetailModel
{
   public int Action { get; set; }
}

<div class="form-group">
  <div class="checkbox">                               
    <b>Select Action Type: </b><br />
    <span> Approved: </span> @Html.RadioButtonFor(m => m.Action, 1)
    <span> | Rejected: </span> @Html.RadioButtonFor(m => m.Action, 2)
    <span> | Pending: </span> @Html.RadioButtonFor(m => m.Action, 0)
    <span> | On Hold: </span> @Html.RadioButtonFor(m => m.Action, 3)       
  </div>
</div>

public ActionResult Index(DetailModel model)
{
  Action action
  string status = "";
  if (model.Action != null && model.Action == 0)
    status = "Pending";
  if (model.Action != null && model.Action == 1)
    status = "Approved";
  if (model.Action != null && model.Action == 2)
    status = "Rejected";
  if (model.Action != null && model.Action == 3)
    status = "On Hold";
  
  action.Status = status 
  _aRepository.Update(action);
}

RADIOBUTTONFOR - USING ENUM

public enum LeadActions
{
   Approved = 1,
   Rejected = 2,
   Pending = 0,
   OnHold = 3           
}
public class DetailModel
{
   public Int64 Id { get; set; }
   public string Name { get; set; }
   public LeadActions LeadAction { get; set; }           
}
@using Articles.Enums;
@using Articles.Models;
@model DetailModel


@using (Html.BeginForm())
{
    <div class="form-group">
        <div class="checkbox">
            Select Action Type: <br />
            <span> @Html.Label("Approved"): </span> @Html.RadioButtonFor(m => m.LeadAction, LeadActions.Approved)
            <span> | @Html.Label("Rejected"): </span> @Html.RadioButtonFor(m => m.LeadAction, LeadActions.Rejected)
            <span> | @Html.Label("Pending"): </span> @Html.RadioButtonFor(m => m.LeadAction, LeadActions.Pending)
            <span> | @Html.Label("OnHold"): </span> @Html.RadioButtonFor(m => m.LeadAction, LeadActions.OnHold)
        </div>
    </div>

    <input type="submit" name="name" value="Submit" />  
}
[HttpGet]
public ActionResult Index()
{
   Lead leads = (_leadRepository.Table.Select(l=> l).FirstOrDefault());
   DetailModel detail = new DetailModel();

   if (leads != null)
   {
      detail.Id = leads.id;
      detail.LeadAction = (LeadActions)System.Enum.Parse(typeof(LeadActions), 
                 leads.Status.ToString());
      detail.Name = leads.Name;
   }
   return View("Radio", detail);
}

[HttpPost]
public ActionResult Index(DetailModel model)
{
   Lead lead = new Lead();
   lead.Status = (int)model.LeadAction;
   _leadRepository.Update(lead);
   return View("Radio", model);
}
Related