0

I haven't worked with radio buttons much and I'm new to MVC.

I'm trying to get the value of selected radio button from the view to the controller Post method. All other element values are coming expect the radio button. They always return false

Here is my code

VIEW

<label for="first" class="col-sm-3 control-label">Patient Name:</label>
<div class="col-sm-3">
  <input type="text" class="form-control" id="first" placeholder="First" name="FirstName">
</div>
<div class="col-sm-2">
  <input type="text" class="form-control" id="middle" placeholder="Middle" name="MiddleName">
</div>
<div class="col-sm-3">
  <input type="text" class="form-control" id="last" placeholder="Last" name="LastName">
</div>
<div class="col-sm-1"></div>
</div>
<div class="form-group">
  <label for="month" class="col-sm-3 control-label">DOB:</label>
  <div class="col-sm-2">
    @Html.DropDownList("MonthofBirth", Acc.Web_Code.viewmodels.Calendar.MonthsSelectList, "Month", new { @class = "form-control", name = "month", id = "month" })
  </div>
  <div class="col-sm-1">
    @Html.DropDownList("DayofBirth", Acc.Web_Code.viewmodels.Calendar.DaysSelectList, "Day", new { @class = "form-control", name = "day", id = "day" })
  </div>
  <label class="radio-inline">                                  
    <input type="radio" name="Gender" id="male" value="Male"> Male
  </label>
  <label class="radio-inline">
    <input type="radio" name="Gender" id="female" value="Female"> Female
  </label>

Post Method

public ActionResult AddNewPatient(AddPView objAddPView)

and my model is

public class AddPView
{
    [Required]
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    [Required]
    public string LastName { get; set; }
    public bool Gender { get; set; }
    [Required]
    public string YearofBirth { get; set; }
    [Required]
    public string MonthofBirth { get; set; }
    [Required]
    public string DayofBirth { get; set; }
    [Required]
    public string Address1 { get; set; }
    [Required]
    public string City { get; set; }
    [Required]
    public string State { get; set; }
    public string Zip { get; set; }
    [Required]
    public string Phone { get; set; }
}

Tried a lot of variation in view but every thing returns false

too_cool
  • 1,164
  • 8
  • 24

2 Answers2

0

Gender is a bool in the model, so the values need to be bool too:

  <label class="radio-inline">                                  
    <input type="radio" name="Gender" id="male" value="true"> Male
  </label>
  <label class="radio-inline">
    <input type="radio" name="Gender" id="female" value="false"> Female
  </label>
thenninger
  • 506
  • 3
  • 16
-2

Try something like this. I have copy it from on e of our projects.

<div class="form-group">
    @Html.LabelFor(model => model.Type, new {@class = "control-label col-md-2"})
    <div class="col-md-8">
        @Html.RadioButtonFor(model => model.Type, ChangeType.Change) Änderung
        @Html.RadioButtonFor(model => model.Type, ChangeType.New) Neuanlage
        @Html.ValidationMessageFor(model => model.Type, "", new {@class = "text-danger"})
    </div>
</div>