我正在嘗試構建一個 HTML 助手,它將像HTML.ForLabel助手一樣工作,但會將標題屬性設置為[Display]注釋中的 description 值。我有工具提示部分工作,但我無法htmlAttributes工作。這是我的助手代碼public static class LabelWithTooltip{ public static MvcHtmlString Tooltip<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes) { var metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData); string htmlFieldName = ExpressionHelper.GetExpressionText(expression); string labelText = metaData.DisplayName ?? metaData.PropertyName ?? htmlFieldName.Split('.').Last(); if (String.IsNullOrEmpty(labelText)) { return MvcHtmlString.Empty; } var label = new TagBuilder("label"); label.Attributes.Add("for", helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName)); if (!string.IsNullOrEmpty(metaData.Description)) { label.Attributes.Add("title", metaData.Description); label.MergeAttributes(htmlAttributes); } label.SetInnerText(labelText); return MvcHtmlString.Create(label.ToString()); }}這是在視圖中調用它的行@Html.Tooltip(model => model.Guid, htmlAttributes: new { @class = "control-label col-md-2" })它沒有采用我為 htmlAttributes任何幫助,將不勝感激。
- 1 回答
- 0 關注
- 212 瀏覽
添加回答
舉報
0/150
提交
取消