Asp.net mvc 2 ASP.NET GetFullHtmlField未返回有效id

Asp.net mvc 2 ASP.NET GetFullHtmlField未返回有效id,asp.net-mvc-2,telerik-mvc,Asp.net Mvc 2,Telerik Mvc,在MVC2模板中,我通常使用this.ViewData.TemplateInfo.getFullHtmlFieldDid(fieldName) 生成html元素的id字段。这在大多数情况下都有效 但是,此方法实际上并没有返回,它只是将ViewData.TemplateInfo.HtmlFieldPrefix作为字段名的前缀,这在呈现在HtmlFieldPrefix中具有[]的集合时会给我带来问题 我一直在手动将这些字符转换为我认为需要的。,但这似乎并不优雅(重复代码),有人找到了正确生成id字段

在MVC2模板中,我通常使用
this.ViewData.TemplateInfo.getFullHtmlFieldDid(fieldName)
生成html元素的id字段。这在大多数情况下都有效

但是,此方法实际上并没有返回,它只是将
ViewData.TemplateInfo.HtmlFieldPrefix
作为
字段名
的前缀,这在呈现在HtmlFieldPrefix中具有
[]
的集合时会给我带来问题


我一直在手动将这些字符转换为我认为需要的
,但这似乎并不优雅(重复代码),有人找到了正确生成id字段的好方法吗?

您能更具体地说明您遇到的问题吗

例如,提供了一种优雅的方法。虽然不使用模板,但在局部视图中仍然保持干燥

虽然id不一致-名称没有问题,但我遇到的唯一问题是,使用jquery.infieldlabel时,标签的for属性(由LabelFor helper内部的GetFullHtmlFieldDid生成)与输入的相应TextBoxID不匹配。因此,我创建了LabelForCollectionItem助手方法,该方法只使用与TextBox相同的方法生成id-
TagBuilder.GenerateId(全名)

也许代码不符合您的需要,但希望它能帮助一些人,因为我在第一次搜索我的问题的解决方案时发现了您的问题

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;
    }
}
公共静态类标签扩展
{
/// 
///生成带有“for”属性的标签,该属性对应于输入所呈现的id(例如TextBoxFor),
///对于输入为集合项(全名包含[])的情况。
///由于存在括号,GetFullHtmlFieldDid在Html.BeginCollectionItem内工作不正确。
///此方法复制TextBox的id生成。
/// 
公共静态MvcHtmlString LabelForCollectionItem(此HtmlHelper html、表达式、,
字符串labelText=null,对象htmlAttributes=null),其中TModel:class
{
var标记=新标记生成器(“标签”);
tag.MergeAttributes(新RouteValueDictionary(htmlAttributes));//将对象转换为IDictionary
//设置内部文本
字符串htmlFieldName=ExpressionHelper.GetExpressionText(表达式);
字符串innerText=labelText??GetDefaultLabelText(html,表达式,htmlFieldName);
if(string.IsNullOrEmpty(innerText))
{
返回MvcHtmlString.Empty;
}
tag.SetInnerText(innerText);
//为属性设置
string forId=GenerateTextBoxId(标记、html、htmlFieldName);
tag.Attributes.Add(“for”,forId);
返回MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
/// 
///从System.Web.Mvc.Html.InputExtensions中提取
/// 
私有静态字符串GenerateTextBoxId(TagBuilder TagBuilder、HtmlHelper html、字符串htmlFieldName)
{
字符串fullName=html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName);
tagBuilder.GenerateId(全名);
string forId=tagBuilder.Attributes[“id”];
tagBuilder.Attributes.Remove(“id”);
返回forId;
}
/// 
///从System.Web.Mvc.Html.LabelExtensions中提取
/// 
私有静态字符串GetDefaultLabelText(HtmlHelper HtmlHelper,表达式表达式,字符串htmlFieldName)
{
var metadata=modelmetada.FromLambdaExpression(表达式,htmlHelper.ViewData);
字符串labelText=metadata.DisplayName??metadata.PropertyName??htmlFieldName.Split('.').Last();
返回标签文本;
}
}

因此,在您的方法中,如果htmlFieldName包含方括号,会发生什么情况?e、 g.“Users[0]”Html.LabelForCollectionItem(x=>x.Users[0].Name)返回这是一种有趣的方法,帮助我为我们的特定情况找到了解决方案。谢谢(如果你能修改你的答案,我会投你一票——所以避开愚蠢的堆栈溢出规则)