ASP.NET MVC using bool value for switch property with switch

Basically, I want my switches to be checked depending on the value from my model view control, below is an example of the code and my attempt:

  <tr>
                <td>
                <label for="brakepads" class="label3">Brake Pads:</label></td>
                <%= Html.TextBox("brakepads", Model.brakepads, new {style="display:none" })%>
                <%= Html.ValidationMessage("brakepads", "*") %>

                <td><label for="brakefluid" class="label4" >Passed:</label>
                <asp:RadioButton ID="RadioButton15" runat="server" GroupName="b" value="True" Checked="<%= Model.brakepads.Value %>" onclick="brakepads.value=(this.value)" /></td>
                <td><label for="brakefluid"  class="label4" >Failed:</label>
                <asp:RadioButton ID="RadioButton16" runat="server" GroupName="b" value="False" Checked="<% Model.brakepads.Value %>" onclick="brakepads.value=(this.value)"/></td>
                </tr>

I get the following at runtime: I cannot create an object of type "System.Boolean" from its string representation "<% = Model.brakepads.Value%>" for the property "Checked".

I tried using (bool) for casting, but that didn't work either, so I'm not sure what to do next. My only problem is that sometimes the bool will be null, if this does not break the functionality, then this is normal.

+3
source share
3 answers

Html.RadioButton, MVC Model.brakepads. javascript "".

0

ASP.NET MVC 3 #

, html

Apply discount: 
<input type="radio" name="apply_discount" value="yes" /> Yes
<input type="radio" name="apply_discount" value="no" /> No

public bool ApplyDiscount { get; set; }

, , html

Apply discount:
@Html.RadioButtonFor(m => m.ApplyDiscount, true) Yes
@Html.RadioButtonFor(m => m.ApplyDiscount, false) No

, .

+6

You mix WebForms ( RadioButton) controls with ASP.NET MVC ( Html.TextBox) helpers , I think this is a dangerous practice.

You can try using a helper method Html.RadioButton()to render the switch:

<td><label for="brakefluid" class="label4" >Passed:</label>
<%= Html.RadioButton("brakefluid", true, Model.brakepads.Value) %></td>
<td><label for="brakefluid"  class="label4" >Failed:</label>
<%= Html.RadioButton("brakefluid", false, !Model.brakepads.Value) %></td>    
+2
source

All Articles