html 如何覆盖,如何覆盖@ Html.LabelFor模板?

I expanded upon balealexandre's answer and added in the ability to specify HTML to include both before and after your label text. I added a bunch of method overloads and comments. I hope this helps folks!

Also snagged information from here: Html inside label using Html helper

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());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 HTML 留言板源码模板: ```html <!DOCTYPE html> <html> <head> <title>留言板</title> </head> <body> <h1>留言板</h1> <!-- 表单 --> <form> <label for="name">姓名:</label> <input type="text" id="name" name="name"><br> <label for="email">邮箱:</label> <input type="email" id="email" name="email"><br> <label for="message">留言:</label><br> <textarea id="message" name="message" rows="5" cols="30"></textarea><br> <input type="submit" value="提交"> </form> <hr> <!-- 留言列表 --> <div id="messages"> <!-- 留言列表会在这里动态生成 --> </div> <!-- JavaScript --> <script> // 获取留言列表 function getMessages() { // 发送 AJAX 请求到服务器 // 代码省略,需要自己实现 // 返回的数据应该是一个数组,每个元素是一个留言对象,例如: // [ // {name: "张三", email: "zhangsan@example.com", message: "留言1"}, // {name: "李四", email: "lisi@example.com", message: "留言2"}, // ... // ] var messages = [ {name: "张三", email: "zhangsan@example.com", message: "留言1"}, {name: "李四", email: "lisi@example.com", message: "留言2"} ]; // 在页面中动态生成留言列表 var html = ""; for (var i = 0; i < messages.length; i++) { html += "<div>"; html += "<strong>" + messages[i].name + "</strong>"; html += "" + messages[i].email + ""; html += "<p>" + messages[i].message + "</p>"; html += "</div>"; } document.getElementById("messages").innerHTML = html; } // 当页面加载完成后,获取留言列表 window.onload = function() { getMessages(); }; </script> </body> </html> ``` 说明: - 这是一个简单的 HTML 页面,包含一个表单和一个留言列表。 - 表单用于提交留言,包含姓名、邮箱和留言内容三个字段。 - 留言列表会在页面加载完成后动态生成,使用 JavaScript 实现。 - `getMessages()` 函数用于获取留言列表,这里只是一个简单的示例,实际使用时需要发送 AJAX 请求到服务器获取数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值