Beacon Button MVC Razor

In a partial view, I work with such text fields.

@model Dictionary<string, string> @Html.TextBox("XYZ", @Model["XYZ"]) 

How can I generate radio buttons and get the desired value as a collection as YES / NO True / False)? I am currently getting null for "ABC" if I select any value for below.

  <label>@Html.RadioButton("ABC", @Model["ABC"])Yes</label> <label>@Html.RadioButton("ABC", @Model["ABC"])No</label> 

controller

  public int Create(int Id, Dictionary<string, string> formValues) { //Something Something } 
+43
c # asp.net-mvc asp.net-mvc-3 razor
May 29 '12 at 19:33
source share
8 answers

To do this for multiple elements, do something like:

 foreach (var item in Model) { @Html.RadioButtonFor(m => m.item, "Yes") @:Yes @Html.RadioButtonFor(m => m.item, "No") @:No } 
+57
May 29 '12 at 19:51
source share

Simply:

  <label>@Html.RadioButton("ABC", True)Yes</label> <label>@Html.RadioButton("ABC", False)No</label> 

But you should always use a strongly typed model, as suggested by cacho.

+22
May 29 '12 at 20:30
source share

I am solving the same problem with this SO answer.

It basically binds the switch to the logical property of a strongly typed model.

 @Html.RadioButton("blah", !Model.blah) Yes @Html.RadioButton("blah", Model.blah) No 

Hope this helps!

+13
May 29 '12 at 19:51
source share

I did it like this:

  @Html.RadioButtonFor(model => model.Gender, "M", false)@Html.Label("Male") @Html.RadioButtonFor(model => model.Gender, "F", false)@Html.Label("Female") 
+8
Oct 21 '15 at 9:47
source share
 <label>@Html.RadioButton("ABC", "YES")Yes</label> <label>@Html.RadioButton("ABC", "NO")No</label> 
+7
May 29 '12 at 20:17
source share

This works for me.

 @{ var dic = new Dictionary<string, string>() { { "checked", "" } }; } @Html.RadioButtonFor(_ => _.BoolProperty, true, (@Model.BoolProperty)? dic: null) Yes @Html.RadioButtonFor(_ => _.BoolProperty, false, (!@Model.HomeAddress.PreferredMail)? dic: null) No 
0
Mar 05 '14 at 1:04
source share
 <p>@Html.RadioButtonFor(x => x.type, "Item1")Item1</p> <p>@Html.RadioButtonFor(x => x.type, "Item2")Item2</p> <p>@Html.RadioButtonFor(x => x.type, "Item3")Item3</p> 
0
Apr 28 '17 at 3:06 on
source share
 <div class="col-md-10"> Male: @Html.RadioButton("Gender", "Male") Female: @Html.RadioButton("Gender", "Female") </div> 
0
Aug 25 '17 at 17:12
source share



All Articles