<tfoot id='0WAUN'></tfoot>

      • <bdo id='0WAUN'></bdo><ul id='0WAUN'></ul>
      <i id='0WAUN'><tr id='0WAUN'><dt id='0WAUN'><q id='0WAUN'><span id='0WAUN'><b id='0WAUN'><form id='0WAUN'><ins id='0WAUN'></ins><ul id='0WAUN'></ul><sub id='0WAUN'></sub></form><legend id='0WAUN'></legend><bdo id='0WAUN'><pre id='0WAUN'><center id='0WAUN'></center></pre></bdo></b><th id='0WAUN'></th></span></q></dt></tr></i><div id='0WAUN'><tfoot id='0WAUN'></tfoot><dl id='0WAUN'><fieldset id='0WAUN'></fieldset></dl></div>
    1. <small id='0WAUN'></small><noframes id='0WAUN'>

        <legend id='0WAUN'><style id='0WAUN'><dir id='0WAUN'><q id='0WAUN'></q></dir></style></legend>
      1. 单选按钮生成重复的 HTML id-s

        时间:2023-10-20

              <bdo id='k1oZd'></bdo><ul id='k1oZd'></ul>
            • <legend id='k1oZd'><style id='k1oZd'><dir id='k1oZd'><q id='k1oZd'></q></dir></style></legend>
                  <tbody id='k1oZd'></tbody>
                <tfoot id='k1oZd'></tfoot>
                <i id='k1oZd'><tr id='k1oZd'><dt id='k1oZd'><q id='k1oZd'><span id='k1oZd'><b id='k1oZd'><form id='k1oZd'><ins id='k1oZd'></ins><ul id='k1oZd'></ul><sub id='k1oZd'></sub></form><legend id='k1oZd'></legend><bdo id='k1oZd'><pre id='k1oZd'><center id='k1oZd'></center></pre></bdo></b><th id='k1oZd'></th></span></q></dt></tr></i><div id='k1oZd'><tfoot id='k1oZd'></tfoot><dl id='k1oZd'><fieldset id='k1oZd'></fieldset></dl></div>

                  <small id='k1oZd'></small><noframes id='k1oZd'>

                  本文介绍了单选按钮生成重复的 HTML id-s的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  似乎默认的 ASP.NET MVC2 Html 帮助程序在使用这样的代码 (EditorTemplates/UserType.ascx) 时会生成重复的 HTML ID:

                  It seems that the default ASP.NET MVC2 Html helper generates duplicate HTML IDs when using code like this (EditorTemplates/UserType.ascx):

                  <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<UserType>" %>
                  
                  <%: Html.RadioButton("", UserType.Primary, Model == UserType.Primary) %>
                  <%: Html.RadioButton("", UserType.Standard, Model == UserType.Standard) %>
                  <%: Html.RadioButton("", UserType.ReadOnly, Model == UserType.ReadOnly) %>
                  

                  它生成的 HTML 是:

                  The HTML it produces is:

                  <input checked="checked" id="UserType" name="UserType" type="radio" value="Primary" /> 
                  <input id="UserType" name="UserType" type="radio" value="Standard" /> 
                  <input id="UserType" name="UserType" type="radio" value="ReadOnly" /> 
                  

                  这清楚地表明了一个问题.所以我一定是在滥用 Helper 什么的.

                  That clearly shows a problem. So I must be misusing the Helper or something.

                  我可以手动将 id 指定为 html 属性,但我不能保证它是唯一的.

                  I can manually specify the id as html attribute but then I cannot guarantee it will be unique.

                  所以问题是如何确保 RadioButton 帮助器生成的 ID 对于每个值都是唯一的,并且仍然保留生成这些 ID 的约定(因此嵌套模型是否受到尊重?(最好不要手动生成 ID.)

                  So the question is how to make sure that the IDs generated by RadioButton helper are unique for each value and still preserve the conventions for generating those IDs (so nested models are respected? (Preferably not generating IDs manually.)

                  推荐答案

                  我遇到了同样的问题.手动指定 ID 似乎是唯一的解决方案.如果您不需要任何 id(如 javascript),但只希望它是唯一的,您可以为它们生成 Guid:

                  I faced the same problem. Specyfying IDs manually seems to be the only solution. If you don't need the ids for anything (like javascript), but want it only to be unique you could generate Guids for them:

                  <%: Html.RadioButton("", UserType.Primary, Model == UserType.Primary, new { id="radio" + Guid.NewGuid().ToString()}) %>
                  

                  更优雅的解决方案是在 HtmlHelper 上创建自己的扩展方法,以将 ID 创建逻辑与视图分开.比如:

                  A more elegant solution would be to create your own extension method on HtmlHelper to separate ID creation logic from the view. Something like:

                  public static class HtmlHelperExtensions
                  {
                  
                      public static MvcHtmlString MyRadioButtonFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, bool value)
                      {
                          string myId = // generate id...
                          return htmlHelper.RadioButtonFor(expression, value, new {id=myId});
                      }
                  }
                  

                  helper 方法可以使用 ViewContext 和 Model 数据来创建更有意义的 ID.

                  The helper method could use ViewContext and Model data to create more meaningfull IDs.

                  更新:

                  如果你使用 EditorTemplates 来呈现这样的控件

                  If you use EditorTemplates to render the control like this

                  <%= Html.EditorFor(m=>m.User, "MyUserControl") %>
                  

                  然后在 MyUserControl.ascx(放置在 ~/Views/Shared/EditorTemplates)中,您可以使用 ViewData.TemplateInfo.HtmlFieldPrefix 属性来访问父控件 ID 或 Html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId("MyPostfixPart") 生成前缀id.这些方法可以在上面的辅助扩展中使用.这同样适用于使用 Html.Editor(...)Html.EditorForModel(...) 呈现的控件.在 Html.Editor 帮助器中,您还可以根据需要手动指定 htmlFiledName.

                  Then inside the MyUserControl.ascx (placed in ~/Views/Shared/EditorTemplates) you can use ViewData.TemplateInfo.HtmlFieldPrefix property to access the parent control ID or Html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId("MyPostfixPart") to generate prefixed id. Theese methods could be used in the helper extension above. The same works with controls rendered with Html.Editor(...) and Html.EditorForModel(...). In the Html.Editor helper you can also specify htmlFiledName manually if you want.

                  当你嵌入控件时

                  <%= Html.Partial("UserControl", Model.User) %>
                  

                  生成有意义的 ID 更加困难,因为 Html.Partial 不会提供有关前缀的信息 - ViewData.TemplateInfo.HtmlFieldPrefix 将始终为空.然后,唯一的解决方案是手动将前缀作为模型字段的 ViewData 键传递给 ascx 控件,这不像前一个解决方案那样优雅.

                  generation of meaningfull IDs is harder because the Html.Partial will not provide information about the prefix - the ViewData.TemplateInfo.HtmlFieldPrefix will be always empty. Then, the only solution would be to pass the prefix manually to the ascx control in as ViewData key of as a model field which is not as elegant a solution as the previous one.

                  这篇关于单选按钮生成重复的 HTML id-s的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 -webkit-backface-visibility 时,Windows Chrome 中的单选按钮背景变为白色 下一篇:如何让输入单选元素水平对齐?

                  相关文章

                1. <small id='lkT6z'></small><noframes id='lkT6z'>

                    <tfoot id='lkT6z'></tfoot>
                    <i id='lkT6z'><tr id='lkT6z'><dt id='lkT6z'><q id='lkT6z'><span id='lkT6z'><b id='lkT6z'><form id='lkT6z'><ins id='lkT6z'></ins><ul id='lkT6z'></ul><sub id='lkT6z'></sub></form><legend id='lkT6z'></legend><bdo id='lkT6z'><pre id='lkT6z'><center id='lkT6z'></center></pre></bdo></b><th id='lkT6z'></th></span></q></dt></tr></i><div id='lkT6z'><tfoot id='lkT6z'></tfoot><dl id='lkT6z'><fieldset id='lkT6z'></fieldset></dl></div>
                        <bdo id='lkT6z'></bdo><ul id='lkT6z'></ul>
                    1. <legend id='lkT6z'><style id='lkT6z'><dir id='lkT6z'><q id='lkT6z'></q></dir></style></legend>