2015-02-09 109 views
0

所以,我想改变我的复选框,已选中和未选中状态的单选按钮,说是,(选中)或否(未选中)。如何将我的复选框更改为单选按钮?

这里就是我所做的复选框:

笔者认为:

@Html.CheckBoxUi("PerpendicularCheckbox",@H.GetString("IsPerpendicular"), null, new { style = "margin-right:10px", @class = "type-style-paragraph" }) 

JS:

$('input:checkbox[name=PerpendicularCheckbox]').on({ 
       "change": function() { 
        if (getUpdate()) { 
         var $this = $(this); 
         if (($this).is(':checked')) 
          $("ul li button").click(); 
        } 
       } 
      }); 

      if (!Perpendicular) {     
       $("#PerpendicularCheckbox").prop("checked", false); 
      } 
      else {    
       $("#PerpendicularCheckbox").prop("checked", true); 
      } 

我想知道什么,我需要将其更改为单选按钮,是和没有选项,在asp.net mvc中使用html扩展?

编辑:

我在单选按钮loosy尝试:

@Html.RadioButtonForUi("PerpendicularCheckbox",@H.GetString("IsPerpendicular"), null, new { style = "margin-right:10px", @class = "type-style-paragraph" }) 



$('input:radio[name=PerpendicularCheckbox]').on({ 
        "change": function() { 
         if (getUpdate()) { 
          var $this = $(this); 
          if (($this).is(':checked')) 
           $("ul li button").click(); 
         } 
        } 
       }); 

RadioButtonForUi:

public static MvcHtmlString RadioButtonForUi<TModel, TProperty>(
      this HtmlHelper<TModel> htmlHelper, 
      Expression<Func<TModel, TProperty>> expression, 
      string name, 
      bool IsEnable, 
      bool IsChecked, 
      object onchange = null, 
      string className = "", 
      bool isRequreid = true 

     ) {etc.....} 
+0

使用它你的意思是这样的吗? https://msdn.microsoft.com/en-us/library/dd493075(v=vs.100).aspx – 2015-02-09 18:18:38

+0

是的,但我只是想知道如何使用@ html.radiobutton选择是和不是为上面的复选框。 – 2015-02-09 18:21:09

+0

为什么不使用@ Html.RadioButtonFor如果你需要单选按钮?! – oqx 2015-02-09 18:22:00

回答

0

这里是一个测试样品:

 <div class="form-group"> 
     @Html.LabelFor(model => model.SaleOfPropertyPurchase, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      <div class="checkbox"> 
       @Html.RadioButtonFor(model => model.SaleOfPropertyPurchase, true, new { id = "SaleOfPropertyPurchase-true" }) Yes 
       @Html.RadioButtonFor(model => model.SaleOfPropertyPurchase, false, new { id = "SaleOfPropertyPurchase-false" }) No 
       @Html.ValidationMessageFor(model => model.SaleOfPropertyPurchase, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
    </div> 

下面是一些示例jquery的反应的单选按钮点击,以及建立在表格上最初显示:

@Scripts.Render("~/bundles/jquery") 
<script type="text/javascript"> 
    $(function() { 
     $('#CurrentPropertyOwner-true').on('change', function() { 
      $('#CurrentProperty').show(); 
     }); 
    }); 
    $(function() { 
     $('#CurrentPropertyOwner-false').on('change', function() { 
      $('#CurrentProperty').hide(); 
     }); 
    }); 

    $(document).ready(function() { 
     var ischecked = $('#CurrentPropertyOwner-true').is(':checked') 
     if (ischecked == true) { 
      $('#CurrentProperty').show(); 
     } 
     var ischecked = $('#CurrentPropertyOwner-false').is(':checked') 
     if (ischecked == true) { 
      $('#CurrentProperty').hide(); 
     } 
    }); 
</script> 
0

你需要使两个单选按钮的属性,一个与“真”的价值和其他与价值“假”,所以选择的值可以绑定到一个boolean

您自定义HTML助手将需要

namespace YourAssembly.Html 
{ 
    public static class MyHelpers 
    { 
    public static MvcHtmlString BooleanButtonsFor<TModel>(this HtmlHelper<TModel> helper, Expression<Func<TModel, bool>> expression) 
    { 
     ModelMetadata metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData); 
     string name = ExpressionHelper.GetExpressionText(expression); 
     StringBuilder html = new StringBuilder(); 
     // Yes button 
     string id = string.Format("{0}-yes", name); 
     html.Append(helper.RadioButtonFor(expression, "True", new { id = id })); 
     html.Append(helper.Label(id, "Yes")); 
     // No button 
     id = string.Format("{0}-no", name); 
     html.Append(helper.RadioButtonFor(expression, "False", new { id = id })); 
     html.Append(helper.Label(id, "No")); 
     // enclode in a div for easier styling with css 
     TagBuilder div = new TagBuilder("div"); 
     div.AddCssClass("radiobuttongroup"); 
     div.InnerHtml = html.ToString(); 
     return MvcHtmlString.Create(div.ToString()); 
    } 
    } 
} 

然后添加一个引用的web.config

<namespaces>
<add namespace="YourAssembly.Html "/> 

,并在视图

@Html.BooleanButtonsFor(m => m.YourBoolProperty)