ASP.NET GetFullHtmlFieldId没有返回有效的id

时间:2010-05-14 23:15:22

标签: asp.net-mvc-2 telerik-mvc

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

但是这个方法实际上并没有返回valid id field,它只是fieldName前缀ViewData.TemplateInfo.HtmlFieldPrefix,这在我在HtmlFieldPrefix中呈现[]的集合时会给我带来问题

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

1 个答案:

答案 0 :(得分:3)

您能否更具体地了解您遇到的问题?

例如,editing variable length list提供了validation support的优雅方法。虽然它不使用模板仍然保持DRY与部分视图。

虽然ID是不一致的 - 名称是正常的,我遇到的唯一问题是使用jquery.infieldlabel似乎标签的属性(由LabelFor helper内的GetFullHtmlFieldId生成)与相应的TextBoxFor输入的id不匹配。所以我创建了LabelForCollectionItem辅助方法,它只使用与文本框相同的id生成方法 - TagBuilder.GenerateId(fullName)

也许代码不符合您的需求,但希望它会帮助某人,因为我在第一次寻找解决问题的方法中找到了您的问题。

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;
    }
}