- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
i.Value, "true")-6ren">
我有这个 Razor 模板
<table>
<tr>
<td>@Html.RadioButtonFor(i => i.Value, "1")</td>
<td>@Html.LabelFor(i => i.Value, "true")</td>
</tr>
<tr>
<td>@Html.RadioButtonFor(i => i.Value, "0")</td>
<td>@Html.LabelFor(i => i.Value, "false")</td>
</tr>
</table>
这给了我这个 HTML
<table>
<tr>
<td><input id="Items_1__Value" name="Items[1].Value" type="radio" value="1" /></td>
<td><label for="Items_1__Value">true</label></td>
</tr>
<tr>
<td><input checked="checked" id="Items_1__Value" name="Items[1].Value" type="radio" value="0" /></td>
<td><label for="Items_1__Value">false</label></td>
</tr>
</table>
所以我有两次 ID Items_1__Value
,这当然不好,并且在浏览器中不起作用,当我单击第二个标签“false”时,第一个 radio 将被激活。
我知道我可以在 RadioButtonFor 添加自己的 Id 并用我的标签引用它,但这不太好,不是吗?特别是因为我处于循环中,不能只使用名称“值”和添加的数字,这最终也会出现在我的最终 HTML 标记中的多个 Dom Id 中。
这不应该是一个好的解决方案吗?
最佳答案
不要为此过度设计解决方案。您想要完成的只是让单选按钮响应文本上的点击。保持简单,只需将单选按钮包裹在标签标签中即可:
<table>
<tr>
<td><label>@Html.RadioButtonFor(i => i.Value, "1")True</label></td>
</tr>
<tr>
<td><label>@Html.RadioButtonFor(i => i.Value, "0")False</label></td>
</tr>
</table>
LabelFor html 帮助程序通常用于从 View 模型上的 Display 属性引入名称(例如“[Display(Name = "Enter your Name")])。
对于单选按钮,该名称并不是特别有用,因为每个单选按钮都有不同的文本行,这意味着无论如何您都必须将文本硬编码到 View 中。
关于asp.net-mvc-3 - 将 Html.RadioButtonFor 和 Html.LabelFor 用于相同的模型但不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8320172/
我在我的 web 项目中使用 RadioButtonFor,但如果用户选择单选按钮的文本,我想使用标签来选择项目,但生成的 html 给了我由于 Radiobuton,多个相同的 #id,我想更改它以
我的 View 模型中有这个属性: public bool IsObligatory { get; set; } 它应该由用户在 post 方法中填写。问题是我检查了第二个单选按钮的默认值。如何删除它
我正在生成单选按钮列表,然后尝试在加载时选择一个选项,如下所示。 View 中的 Foreach 循环 @foreach (var myListItem in Model.MyList) {
不确定标题是否有意义,但是当我们选择是/否单选按钮时,我有下面的代码来隐藏/显示一些字段 @if (Model != null) { using (Html.BeginForm("NextB
我有一个 PDF 类 public class UIClonePDFDetail { public int CatalogueID { get; set; }
我正在尝试将使用 for 循环和 Razor 语法创建的单选按钮组合在一起。这是代码: @for (var i = 0; i it.Sessions[i].Id)
我在使用模型中的 HtmlHelper、RadioButtonFor 和枚举时遇到问题。我有一个强类型 View ,我希望复选框能够切换我的枚举属性。 Enum.cs public enum Valu
在 ASP.NET MVC4 的 Razor View 中,如何确保没有一个单选按钮默认为未选中? MVC 似乎为我检查了一个,即使我没有声明在以下代码中检查一个。 @Html.RadioButton
这是我的静态类: using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; us
我在 View 中使用 foreach 循环来显示一些单选按钮行.. sample radiobutton Integrity @Html.RadioButton
我使用 @Html.RadioButtonFor 扩展方法有一个奇怪的行为。我正在使用 foreach 循环创建 RadioButton 和 By 三元运算符的列表。我试图将尊重条件的人设置为检查,但
我的 mvc 网页中有 2 个单选按钮。 // some html codes m.Agents, "A", new { Checked = "checked" })%> //ra
我是 MVC 的新手。目前我有一个强类型 View ,我使用两个 Html.RadioButtonFor 作为标签,代码类似于: @Html.RadioButtonFor(model
到目前为止,我正在尝试用 Razor 语法制作一个单选按钮列表 @foreach (var p in Model) { @Html.RadioButton("name", "
我在 View 中嵌套了 foreach 循环,但单选按钮有问题。 @foreach (var header in Model.surveyHeaders) { @header.Name fo
我有三个单选按钮,我的字段值类型是整数,例如 Maintenance 为 3,1 人活跃,2 人不活跃。 @Html.RadioButtonFor(model => model.StatusId, "
我有一个表格: @using (Html.BeginForm("Buy", "Keys", FormMethod.Post)) { 1. @Html.DropDownLi
我有这样定义的单选按钮 @foreach (var actionType in partActionTypes) { @Html.RadioButtonFor(x =>
根据我的研究,在 MVC 中编写复选框的正确方法如下。 @Html.CheckBoxFor(m => m.RememberMe) @Html.LabelFor(m => m.RememberMe) 这
我在 MVC razor 中编写了一个单选按钮。下面是它的代码。 @{ var LinkRadioName = "_Link"; var Content
我是一名优秀的程序员,十分优秀!