添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

我扩展了balealexandre的答案,并添加了指定HTML的功能,以在标签文本之前和之后都包含HTML。 我添加了一堆方法重载和注释。 希望对大家有帮助!

还从这里获取信息:使用HTML帮助器在标签内部的HTML

namespace System.Web.Mvc.Html

public static class LabelExtensions

/// Creates a Label with custom Html before the label text. Only starting Html is provided.

/// Html to preempt the label text.

/// MVC Html for the Label

public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, Func startHtml)

return LabelFor(html, expression, startHtml, null, new RouteValueDictionary("new {}"));

/// Creates a Label with custom Html before the label text. Starting Html and a single Html attribute is provided.

/// Html to preempt the label text.

/// A single Html attribute to include.

/// MVC Html for the Label

public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, Func startHtml, object htmlAttributes)

return LabelFor(html, expression, startHtml, null, new RouteValueDictionary(htmlAttributes));

/// Creates a Label with custom Html before the label text. Starting Html and a collection of Html attributes are provided.

/// Html to preempt the label text.

/// A collection of Html attributes to include.

/// MVC Html for the Label

public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, Func startHtml, IDictionary htmlAttributes)

return LabelFor(html, expression, startHtml, null, htmlAttributes);

/// Creates a Label with custom Html before and after the label text. Starting Html and ending Html are provided.

/// Html to preempt the label text.

/// Html to follow the label text.

/// MVC Html for the Label

public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, Func startHtml, Func endHtml)

return LabelFor(html, expression, startHtml, endHtml, new RouteValueDictionary("new {}"));

/// Creates a Label with custom Html before and after the label text. Starting Html, ending Html, and a single Html attribute are provided.

/// Html to preempt the label text.

/// Html to follow the label text.

/// A single Html attribute to include.

/// MVC Html for the Label

public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, Func startHtml, Func endHtml, object htmlAttributes)

return LabelFor(html, expression, startHtml, endHtml, new RouteValueDictionary(htmlAttributes));

/// Creates a Label with custom Html before and after the label text. Starting Html, ending Html, and a collection of Html attributes are provided.

/// Html to preempt the label text.

/// Html to follow the label text.

/// A collection of Html attributes to include.

/// MVC Html for the Label

public static MvcHtmlString LabelFor(this HtmlHelper html, Expression> expression, Func startHtml, Func endHtml, IDictionary 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());

我扩展了balealexandre的答案,并添加了指定HTML的功能,以在标签文本之前和之后都包含HTML。 我添加了一堆方法重载和注释。 希望对大家有帮助!还从这里获取信息:使用HTML帮助器在标签内部的HTMLnamespace System.Web.Mvc.Html{public static class LabelExtensions{/// Creates a Label with cu...
@ Html 帮助器简单说明,记录些基本的跟 HTML 中对应的@ html 帮助器,@ Html 基本包含了 html 中的表单控件和常用 Html 在@ Html 中,带有For的主要是针对强类型的 Html 类型。 用于说明@ Html 中标签,定义Student对象用于辅助说明, public class Student /// 姓名
在使用@ Html .Lable()来显示Model的某一个字符串属性时,如果该字符串中包含".",那么就会在最终呈现时被截掉开头至"."位置的字符。什么原因尚不清楚。如图: 比如title=“南沙去年给融资租赁企业奖励补贴超1.1亿元”,那么在呈现的时候就会变成“1亿元”,如图: 转载于:https://www.cnblogs.com/wangw...
上一篇:Django之--网页展示Hello World!初步说明了如何使用Django来显示hello world,本文略微进阶下使用 html 模板 来展示hello world~ 首先在mysite同级目录创建templates目录,用于专门存放 html 模板 : [root@python mysite]# tree . ├── db.sqlite3 ├── manage.py