我希望显示和保存db性别像“女性”,因为它的值是“女性”从单选按钮,但它显示不同的值

问题描述 投票:-2回答:2

我想从单选按钮显示性别作为其值,但它获取整个视图 这是观点:

<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"

c# asp.net-mvc
2个回答
0
投票

单选按钮的第二个参数是它的值。请参考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属性。


0
投票

试试这样;



@Html.RadioButtonFor(model => model.gender, "Male", new { @checked = true }) 
@Html.RadioButtonFor(model => model.gender, "Female", new { @checked = true })

© www.soinside.com 2019 - 2024. All rights reserved.