ASP.NET GetFullHtmlFieldId不返回有效身份证件 [英] ASP.NET GetFullHtmlFieldId not returning valid id
问题描述
在MVC2模板,我通常使用 this.ViewData.TemplateInfo.GetFullHtmlFieldId(字段名)
生成一个HTML元素的ID字段。这已在大多数情况下工作。
In MVC2 template, I normally use this.ViewData.TemplateInfo.GetFullHtmlFieldId(fieldName)
to generate the id field of an html element. This has worked in most of the cases.
然而,这种方法实际上并不返回<一href=\"http://stackoverflow.com/questions/1482925/rendering-the-field-name-in-an-editortemplate-rendered-through-editorfor\">valid id字段,它只是preFIX 字段名
与 ViewData.TemplateInfo.HtmlField preFIX
这使集合时导致的问题,我有 []
在HtmlField preFIX。
However this method does not actually return valid id field, it merely prefix fieldName
with ViewData.TemplateInfo.HtmlFieldPrefix
, this is causing problems for me when rendering collections which has []
in the HtmlFieldPrefix.
我已经手动转换这些字符为 _
在哪里找到需要,但是这似乎不优雅(重复code)中,有没有人发现了一个好办法,妥善产生id字段?
I have been manually converting those characters to _
where I find the need, but this seems to be not elegant (repeated code), has anyone found a good way to generate id field properly?
推荐答案
你能具体谈谈什么样的问题,你有吗?
Can you be more specific about the kind of problems do you have?
例如,有一种优雅的方法来的编辑可变长度的列表与<一个href=\"http://blog.stevensanderson.com/2010/01/28/validating-a-variable-length-list-aspnet-mvc-2-style/\"相对=nofollow>验证支持提供。虽然它不使用模板仍保持干燥与部分景色。
For example, there is an elegant approach to editing variable length list with validation support provided. While it doesn't use templates still remains DRY with partial views.
虽然IDS不一致 - 名字确定,只有我遇到的问题是,使用jquery.infieldlabel就显得标签的for属性(由GetFullHtmlFieldId LabelFor帮手内部产生的)不匹配相应TextBoxFor输入ID。所以,我创建了一个只是用来ID生成同样的方法LabelForCollectionItem helper方法作为TextBox - TagBuilder.GenerateId(全名)
While the ids are inconsistent - the names are OK and only problem I encountered is that using jquery.infieldlabel it appeared that label's for attribute (generated by GetFullHtmlFieldId inside LabelFor helper) didn't match id of the appropriate TextBoxFor input. So I created LabelForCollectionItem helper method that just uses the same method for id generation as the TextBox - TagBuilder.GenerateId(fullName)
也许code不符合您的需要,但希望这将帮助别人,因为我找到了解决我的问题第一搜索中你的问题。
Maybe the code doesn't correspond to your need but hope it will help somebody since I found your question among the first searching for solution to my problem.
public static class LabelExtensions
{
/// <summary>
/// Generates Label with "for" attribute corresponding to the id rendered by input (e.g. TextBoxFor),
/// for the case when input is a collection item (full name contains []).
/// GetFullHtmlFieldId works incorrect inside Html.BeginCollectionItem due to brackets presense.
/// This method copies TextBox's id generation.
/// </summary>
public static MvcHtmlString LabelForCollectionItem<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression,
string labelText = null, object htmlAttributes = null) where TModel : class
{
var tag = new TagBuilder("label");
tag.MergeAttributes(new RouteValueDictionary(htmlAttributes)); // to convert an object into an IDictionary
// set inner text
string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
string innerText = labelText ?? GetDefaultLabelText(html, expression, htmlFieldName);
if (string.IsNullOrEmpty(innerText))
{
return MvcHtmlString.Empty;
}
tag.SetInnerText(innerText);
// set for attribute
string forId = GenerateTextBoxId(tag, html, htmlFieldName);
tag.Attributes.Add("for", forId);
return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
/// <summary>
/// Extracted from System.Web.Mvc.Html.InputExtensions
/// </summary>
private static string GenerateTextBoxId<TModel>(TagBuilder tagBuilder, HtmlHelper<TModel> html, string htmlFieldName)
{
string fullName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName);
tagBuilder.GenerateId(fullName);
string forId = tagBuilder.Attributes["id"];
tagBuilder.Attributes.Remove("id");
return forId;
}
/// <summary>
/// Extracted from System.Web.Mvc.Html.LabelExtensions
/// </summary>
private static string GetDefaultLabelText<TModel, TValue>(HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, string htmlFieldName)
{
var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
return labelText;
}
}
这篇关于ASP.NET GetFullHtmlFieldId不返回有效身份证件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!