din*_*tom 3 c# asp.net-mvc radio-button
我对MVC开始从网络表单迁移有点新,所以请耐心等待.
我有一个性别定义为的单选按钮组;
<div class="form-group">
@Html.LabelFor(m => m.Gender, new { @class = "col-xs-3 control-label" })
<div class="col-xs-5">
<div class="radio">
<label>
@Html.RadioButtonFor(m => m.Gender, new { @class = "form-control", value= Gender.Male})<span>Male</span>
@*<input type="radio" name="gender" value="male"/> Male*@
</label>
</div>
<div class="radio">
<label>
@Html.RadioButtonFor(m => m.Gender, new { @class = "form-control", value = Gender.Female }) <span>Female</span >
@*<input type="radio" name="gender" value="female"/> Female*@
</label>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
Gender是另一个项目中定义的枚举,但viewmodel类使用该枚举.
[Required]
[Display(Name = "Gender")]
public DomainClasses.Enums.Gender Gender { get; set; }
Run Code Online (Sandbox Code Playgroud)
无论我似乎试图做到这一点,我得到以下验证错误.
值'{class = form-control,value = Male}'对于Gender不起作用.
更新:
提交之前的原始外观是
但是在改用建议的方法后,为什么现在看起来呢?
看看这张图片
这个图像显示,接受第二个参数作为Html属性没有重载,所以你需要这样写:
<div class="form-group">
@Html.LabelFor(m => m.Gender, new { @class = "col-xs-3 control-label" })
<div class="col-xs-5">
<div class="radio">
<label>
@Html.RadioButtonFor(m => m.Gender, Gender.Male , new { @class = "form-control" })<span>Male</span>
@*<input type="radio" name="gender" value="male"/> Male*@
</label>
</div>
<div class="radio">
<label>
@Html.RadioButtonFor(m => m.Gender, Gender.Female, new { @class = "form-control" }) <span>Female</span >
@*<input type="radio" name="gender" value="female"/> Female*@
</label>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
在此重载,要传递的第二个参数的值的单选按钮
| 归档时间: |
|
| 查看次数: |
562 次 |
| 最近记录: |