如何在Html.Label標籤內添加內聯html元素?Html標籤內部使用Html幫助器
9
A
回答
19
看起來像一個自定義的助手良好的情景:
public static class LabelExtensions
{
public static MvcHtmlString LabelFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> ex,
Func<object, HelperResult> template
)
{
var htmlFieldName = ExpressionHelper.GetExpressionText(ex);
var for = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName);
var label = new TagBuilder("label");
label.Attributes["for"] = TagBuilder.CreateSanitizedId(for);
label.InnerHtml = template(null).ToHtmlString();
return MvcHtmlString.Create(label.ToString());
}
}
然後:
@Html.LabelFor(
x => x.Name,
@<span>Hello World</span>
)
UPDATE:
要達到什麼樣的,你在評論部分問你可能會嘗試以下操作:
public static class HtmlHelperExtensions
{
public static MvcHtmlString LabelFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> ex, Func<object, HelperResult> template)
{
var htmlFieldName = ExpressionHelper.GetExpressionText(ex);
var propertyName = htmlFieldName.Split('.').Last();
var label = new TagBuilder("label");
label.Attributes["for"] = TagBuilder.CreateSanitizedId(htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName));
label.InnerHtml = string.Format(
"{0} {1}",
propertyName,
template(null).ToHtmlString()
);
return MvcHtmlString.Create(label.ToString());
}
}
然後:
@Html.LabelFor(
x => x.Name,
@<em>mandatory</em>
)
2
你將不得不編寫自己的幫手。內置的Html.Label
幫助程序會自動HTML編碼labelText
參數。
2
我借在Darin的答案,並添加到它。我在標籤文本和標籤文本之後添加了標籤文本和html之前的Html功能。我還添加了一堆重載方法和評論。
我也得到了這個職位的一些信息:How can I override the @Html.LabelFor template?
如果希望幫助鄉親。
namespace System.Web.Mvc.Html
{
public static class LabelExtensions
{
/// <summary>Creates a Label with custom Html before the label text. Only starting Html is provided.</summary>
/// <param name="startHtml">Html to preempt the label text.</param>
/// <returns>MVC Html for the Label</returns>
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, Func<object, HelperResult> startHtml)
{
return LabelFor(html, expression, startHtml, null, new RouteValueDictionary("new {}"));
}
/// <summary>Creates a Label with custom Html before the label text. Starting Html and a single Html attribute is provided.</summary>
/// <param name="startHtml">Html to preempt the label text.</param>
/// <param name="htmlAttributes">A single Html attribute to include.</param>
/// <returns>MVC Html for the Label</returns>
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, Func<object, HelperResult> startHtml, object htmlAttributes)
{
return LabelFor(html, expression, startHtml, null, new RouteValueDictionary(htmlAttributes));
}
/// <summary>Creates a Label with custom Html before the label text. Starting Html and a collection of Html attributes are provided.</summary>
/// <param name="startHtml">Html to preempt the label text.</param>
/// <param name="htmlAttributes">A collection of Html attributes to include.</param>
/// <returns>MVC Html for the Label</returns>
public static MvcHtmlString LabelFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, Func<object, HelperResult> startHtml, IDictionary<string, object> htmlAttributes)
{
return LabelFor(html, expression, startHtml, null, htmlAttributes);
}
/// <summary>Creates a Label with custom Html before and after the label text. Starting Html and ending Html are provided.</summary>
/// <param name="startHtml">Html to preempt the label text.</param>
/// <param name="endHtml">Html to follow the label text.</param>
/// <returns>MVC Html for the Label</returns>
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, Func<object, HelperResult> startHtml, Func<object, HelperResult> endHtml)
{
return LabelFor(html, expression, startHtml, endHtml, new RouteValueDictionary("new {}"));
}
/// <summary>Creates a Label with custom Html before and after the label text. Starting Html, ending Html, and a single Html attribute are provided.</summary>
/// <param name="startHtml">Html to preempt the label text.</param>
/// <param name="endHtml">Html to follow the label text.</param>
/// <param name="htmlAttributes">A single Html attribute to include.</param>
/// <returns>MVC Html for the Label</returns>
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, Func<object, HelperResult> startHtml, Func<object, HelperResult> endHtml, object htmlAttributes)
{
return LabelFor(html, expression, startHtml, endHtml, new RouteValueDictionary(htmlAttributes));
}
/// <summary>Creates a Label with custom Html before and after the label text. Starting Html, ending Html, and a collection of Html attributes are provided.</summary>
/// <param name="startHtml">Html to preempt the label text.</param>
/// <param name="endHtml">Html to follow the label text.</param>
/// <param name="htmlAttributes">A collection of Html attributes to include.</param>
/// <returns>MVC Html for the Label</returns>
public static MvcHtmlString LabelFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, Func<object, HelperResult> startHtml, Func<object, HelperResult> endHtml, IDictionary<string, object> htmlAttributes)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
//Use the DisplayName or PropertyName for the metadata if available. Otherwise default to the htmlFieldName provided by the user.
string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
if (String.IsNullOrEmpty(labelText))
{
return MvcHtmlString.Empty;
}
//Create the new label.
TagBuilder tag = new TagBuilder("label");
//Add the specified Html attributes
tag.MergeAttributes(htmlAttributes);
//Specify what property the label is tied to.
tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName));
//Run through the various iterations of null starting or ending Html text.
if (startHtml == null && endHtml == null) tag.InnerHtml = labelText;
else if (startHtml != null && endHtml == null) tag.InnerHtml = string.Format("{0}{1}", startHtml(null).ToHtmlString(), labelText);
else if (startHtml == null && endHtml != null) tag.InnerHtml = string.Format("{0}{1}", labelText, endHtml(null).ToHtmlString());
else tag.InnerHtml = string.Format("{0}{1}{2}", startHtml(null).ToHtmlString(), labelText, endHtml(null).ToHtmlString());
return MvcHtmlString.Create(tag.ToString());
}
}
}
1
爲了滿足SOC與固體原理,代碼可以被增強以下面的代碼:
@Html.LabelFor(m => m.Phone,true)
到:
public static MvcHtmlString LabelFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> ex,bool applyStylingHtml)
{
var metadata = ModelMetadata.FromLambdaExpression(ex, htmlHelper.ViewData);
string displayName = metadata.DisplayName;
string description= metadata.Description;
if (String.IsNullOrEmpty(displayName))
{
return MvcHtmlString.Empty;
}
var sb = new StringBuilder();
sb.Append(displayName);
var htmlFieldName = ExpressionHelper.GetExpressionText(ex);
var propertyName = htmlFieldName.Split('.').Last();
var tag = new TagBuilder("label");
tag.Attributes["for"] = TagBuilder.CreateSanitizedId(htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName));
tag.SetInnerText(sb.ToString());
//Func<object, HelperResult> template='<em>';
HtmlString nestedHtml=new HtmlString("<em>"+description+"</em>");
tag.InnerHtml = string.Format(
"{0} {1}",
tag.InnerHtml,
nestedHtml
);
return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
然後在剃刀代碼中使用它使一切更加動態化,描述屬性應該應用在Model類上,然後HtmlHelper將抓取描述作爲應用「em」的文本Html標籤:
[Display(Name ="Phone",Description = "should be included extention")]
public string Phone { get; set; }
剛擡起頭,你需要通過添加導入自定義的HtmlHelper命名空間的觀點:
@using yourNamespace
相關問題
- 1. CakePHP標準標籤與HTML幫助
- 2. 幫助紅寶石與HTML標籤
- 3. 幫助中選擇標籤在HTML
- 4. greasemonkey幫助(替換html標籤)
- 5. 如何使用BeautifulSoup解析HTML標籤內部的HTML標籤的內容?
- 6. 標籤內部HTML的jQuery過濾器
- 7. javascript幫助使用html和計算器
- 8. 混合C#和HTML幫助器標記
- 9. 幫助使用html替換
- 10. 顯示使用HTML幫助
- 11. 標籤使用ASP.NET HTML輔助
- 12. Ajax調用不會刷新內部的HTML與PHP的功能,使HTML - 幫助?
- 13. Webform中的HTML幫助器?
- 14. Ext.TabPanel HTML幫助
- 15. HTML使用標籤
- 16. 使用HTML標籤
- 17. Symfony2:標籤內部的自定義HTML
- 18. 其內部指令HTML標籤AngularJS
- 19. MVC Html助手使標籤可編輯
- 20. 使用ViewBag不使用HTML幫助
- 21. 獲取html標籤內/ html標籤之間的所有內容
- 22. 用標記幫助程序替換HTML幫助程序
- 23. 使用php獲取HTML標籤內容
- 24. 在html標籤內使用jquery?
- 25. 使用sed替換HTML標籤內容
- 26. 在HTML標籤內使用PHP變量?
- 27. sup標籤內標題標籤html
- 28. HTML/JSP/JAVASCRIPT幫助
- 29. xsl幫助html表
- 30. 幫助AJAX和HTML
@達林 - 季米特洛夫阿爽,我怎樣才能得到它寫的名稱屬性?我想要的結果是 – Marcus 2011-04-18 19:25:25
@馬庫斯,請參閱我的更新。 – 2011-04-18 19:31:50
@ darin-dimitrov,非常感謝您的幫助,但我認爲我有點不清楚。我稱爲PropertyName的標籤值應該是我的屬性上的display屬性中指定的值,或者與正常標籤相同的名稱。再次感謝您的幫助 – Marcus 2011-04-18 19:53:58