更改MVC单选按钮的Id或触发jQuery函数



在MVC Radiobuttonfor helper中使用以下符号,我可以创建一个单选,如果模型中存在一个值,它将有一个默认按钮被选中。

@Html.RadioButtonFor(x => x.Username, "Y", Model.Username == "Y" ? new { Checked = "checked", id = "YUserName" } : null) No: 
@Html.RadioButtonFor(x => x.Username, "N", Model.Username == "N" ? new { Checked = "checked" } : null)

但是,id = "YUserName"不应用于值为"Y"的单选按钮

如果我在单选按钮helper中没有求值表达式,我可以用值"Y"更改单选的id,即:

@Html.RadioButtonFor(x => x.UsePriorFinancialInfo, "Y", new { id = "YUsePriorFinancialInfo" }) 
@Html.RadioButtonFor(x => x.UsePriorFinancialInfo, "N")

是否有一种方法可以将自定义Id应用于具有评估表达式的单选按钮之一?如果没有,是否有一种方法,我可以触发一个jQuery函数,如果单选按钮被选中?——谢谢

首先,移除Checked = "checked"RadioButtonFor助手将分配checked的值。这对我很好

@Html.RadioButtonFor(x => x.Username, "Y", Model.Username == "Y" ? new { id = "YUserName" } : null) Yes:
@Html.RadioButtonFor(x => x.Username, "N") No:
并呈现以下html
<input checked="checked" id="YUserName" name="Username" type="radio" value="Y">
<span>Yes</span>
<input id="Username" name="Username" type="radio" value="N">
<span>No</span>

你确定属性Username的值被设置为"Y"吗?

最新更新