我有onoffswitch-checkbox样式,如果我不写“class=form-control”,控制器返回空值。
我查看的所有示例和视频说明中都使用了Class=form-control
,但我找不到这样做的原因以及不同的使用方法。
@using (Html.BeginForm("actionname", "controllername", FormMethod.Post))
{
<input type="checkbox" class="onoffswitch-checkbox" name="kisikayit" />
}
提前致谢。
bu benim 复选框 stilim
<style type="text/css">
.onoffswitch {
position: relative;
width: 82px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
}
.onoffswitch-checkbox {
display: none;
}
.onoffswitch-label {
display: block;
overflow: hidden;
cursor: pointer;
border: 2px solid #FFFFFF;
border-radius: 20px;
}
.onoffswitch-inner {
display: block;
width: 200%;
margin-left: -100%;
transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
display: block;
float: left;
width: 50%;
height: 28px;
padding: 0;
line-height: 28px;
font-size: 12px;
color: white;
font-family: Trebuchet, Arial, sans-serif;
font-weight: bold;
box-sizing: border-box;
}
.onoffswitch-inner:before {
content: "EVET";
padding-left: 12px;
background-color: #51C234;
color: #FFFFFF;
}
.onoffswitch-inner:after {
content: " HAYIR";
padding-right: 12px;
background-color: #DDDDDD;
color: #FFFFFF;
text-align: right;
}
.onoffswitch-switch {
display: block;
width: 16px;
margin: 6px;
background: #FFFFFF;
position: absolute;
top: 0;
bottom: 0;
right: 43px;
border: 2px solid #FFFFFF;
border-radius: 20px;
transition: all 0.3s ease-in 0s;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
right: 0px;
}
</style>
}
Class=form-control
与你的业务逻辑无关。这个特定的类与您的表单设计有关。如果您不使用它,您的表单设计在表单内的对齐方面可能会看起来很奇怪。
至于控制器中没有获取值。您似乎没有在 UI 中附加任何 ViewModel。当您使用 RAZOR html 帮助程序时,您必须附加 ViewModel,除非您使用 JavaScript 手动发布表单值。这是例子,即
您的 MyViewModel.cs 类文件
public class MyViewModel
{
...
public bool IsToggle {get; set;}
...
}
更新您的 HTML 即
@model MyViewModel
@using (Html.BeginForm("actionname", "controllername", FormMethod.Post))
{
<label>
@Html.CheckBoxFor(m => m.IsToggle, new {class = "onoffswitch-checkbox", name = "kisikayit"})  
<label>Switch</label>
</label>
}