asp.net-mvc – LabelFor和TextBoxFor不生成相同的id
|
使用以下代码时,字段的id和标签的for属性中的id不相同. <%: Html.LabelFor(x => x.Localizations["en"]) %> => Localizations[en]
<%: Html.TextBoxFor(x=> x.Localizations["en"]) %> => Localizations_en_
<%: Html.LabelFor(x => x.Localizations["en"].Property) %>
=> Localizations[en]_Property
<%: Html.TextBoxFor(x=> x.Localizations["en"].Property) %>
=> Localizations_en__Property
我在反射器中跟踪代码,发现生成值的方式不同.不使用相同的帮助方法. LabelFor使用HtmlHelper.GenerateIdFromName和TextBoxFor使用TagBuilder#GenerateId. 有没有人知道这个的原因,或解决方法(除了编写你自己的整套输入/ textarea /选择助手)?或者这是一个错误? 更新: 因为我总是使用标签文本的第二个参数为标签使用html帮助器,我修改它以使用与表单字段助手相同的id生成代码. public static MvcHtmlString LabelFor<TModel,TValue>(this HtmlHelper<TModel> helper,Expression<Func<TModel,TValue>> expression,string labelText)
{
// The main part of this code is taken from the internal code for Html.LabelFor<TModel,TValue>(...).
var metaData = ModelMetadata.FromLambdaExpression(expression,helper.ViewData);
var fieldName = ExpressionHelper.GetExpressionText(expression);
TagBuilder builder = new TagBuilder("label");
// Generate the id as for the form fields (adds it to self).
builder.GenerateId(fieldName);
// Use the generated id for the 'for' attribute.
builder.Attributes.Add("for",builder.Attributes["id"]);
// Remove the id again.
builder.Attributes.Remove("id");
builder.SetInnerText(labelText);
return MvcHtmlString.Create(builder.ToString());
}
这解决了我的直接问题,但它没有回答为什么实现看起来像在MVC2中的问题.如果有原因的话. 顺便说一下:没有必要在HTML5中实际修改id / for属性,因为如果你愿意的话,让id看起来像^[]是完全合法的.所有主流浏览器都支持它. Mathias Bynens很好地解释了这一点. 更新2: 这实际上并不能解决问题,因为DefaultModelBinder无论如何都无法绑定它. MVC 2中的字段名称生成器似乎不支持在字典中使用嵌套对象,因为它生成: <input type="text" name="Dict[en]" value="(VALUE)"> 而不是模型绑定器想要的东西: <input type="hidden" name="Dict[0].Key" value="en"> <input type="text" name="Dict[0].Value" value="(VALUE)"> 奇怪的是它以这种方式开箱即用. 我已经尝试为它创建一个自定义模型绑定器,但无论我尝试使用它,我都无法使用MVC2: ModelBinders.Binders.Add(typeof(IDictionary<string,object>),new DictionaryModelBinder()); ModelBinders.Binders.Add(typeof(IDictionary<string,string>),new DictionaryModelBinder()); ModelBinders.Binders.Add(typeof(IDictionary),new DictionaryModelBinder()); ModelBinders.Binders.Add(typeof(Dictionary),new DictionaryModelBinder()); 所以现在它似乎又回到手动创建带有隐藏.Key字段的名称属性值. 解决方法这是我们计划修复下一个版本(MVC 3 RTM)的MVC3中的一个错误. LabelFor将通过tagbuilder生成’for’属性,使用与生成id相同的逻辑,以便它们排列为数组和嵌套类型.我们目前正在使用html 4.01规范生成ID,因此您不能使用以非字母开头的ID.我们将考虑标准发生变化时应采取的最佳方法. (编辑:长春站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 什么是强类型的视图在ASP.NET MVC
- Asp.net配合easyui实现返回json数据实例
- ASP.Net核心 – 获取帖子表格的所有数据
- asp.net-mvc – 如何将ASP.Net MVC路径段中的1或0映射到布尔
- 模型绑定 – WebApi2:自定义参数绑定以绑定部分参数
- asp.net-mvc – 如何在ASP.NET MVC4中使用具有唯一标识符UR
- asp.net 分页显示数据表的数据的代码
- asp.net-mvc – 有条件地在webgrid中显示图像 – mvc 3
- 如何使用ASP.NET MVC Web API OData链接到Razor中的OData集
- asp.net-mvc – 应用程序服务层作为静态类
- asp.net-web-api – MaxExpansionDepth,带有最新
- asp.net-core – 如何在Visual Studio 2015 RC中
- asp.net – IE8 Win7 Facebook Connect问题
- 如何设置特定于ASP.NET请求的log4net上下文属性?
- .net – TagBuilder从MVC 3 beta版转到RC
- 将搜索框添加到ASP.Net动态数据列表页面
- ASP.net Substitution 页面缓存而部分不缓存的实
- asp.net-mvc – 如何从剃刀视图访问My.Resources
- asp.net – 避免在web.config中提供服务器连接字
- 在IIS / ASP.Net中的.NET 1.1应用程序中创建.NET
