我想从单选按钮显示性别作为其值,但它获取整个视图 这是观点:
<div class="form-inline">
<label for="inputEmail1" class=" col-lg-2 col-sm-2 control-label">Gender<font color="red">*</font></label>
<div class="col-lg-2">
@Html.RadioButtonFor(model => model.Gender, new { @type = "radio", @name = "Male", value = "Male" })Male
</div>
<div class="col-lg-2">
@Html.RadioButtonFor(model => model.Gender, new { @type = "radio", @name = "Female", @value = "Female" })Female
</div>
<div class="col-lg-2">
@Html.RadioButtonFor(model => model.Gender, new { @type = "radio", @name = "Other", @value = "Other" }) Other
</div>
</div>
it is save in database like this i want only Gender Value like "Female"
单选按钮的第二个参数是它的值。请参考here获取文档。但是你传递的是htmlAttributes而不是Value,这就是你在数据库中插入htmlAttributes的原因。
像这样使用。
<label for="inputEmail1" class=" col-lg-2 col-sm-2 control-label">Gender<font color="red">*</font></label>
<div class="col-lg-2">
@Html.RadioButtonFor(model => model.Gender, "Male") Male
</div>
<div class="col-lg-2">
@Html.RadioButtonFor(model => model.Gender, "Female") Female
</div>
<div class="col-lg-2">
@Html.RadioButtonFor(model => model.Gender, "Other") Other
</div>
此外,您不需要指定@type="radio"
,因为HTML Helper方法@Html.RadioButtonFor()
将默认呈现<input type="radio" />
。
正如@Stephen Muecke在评论中提到的那样,您也不需要指定@name属性。
试试这样;
@Html.RadioButtonFor(model => model.gender, "Male", new { @checked = true })
@Html.RadioButtonFor(model => model.gender, "Female", new { @checked = true })