Asp.net mvc 如何在javascript asp.net mvc中向html.label添加内容
我有一个标签 我想在运行时将内容添加到标签中,不幸的是,它不需要任何其他参数来为其创建id属性。我不能为它创建类似于asp:label的id属性吗 谢谢Asp.net mvc 如何在javascript asp.net mvc中向html.label添加内容,asp.net-mvc,asp.net-mvc-2,Asp.net Mvc,Asp.net Mvc 2,我有一个标签 我想在运行时将内容添加到标签中,不幸的是,它不需要任何其他参数来为其创建id属性。我不能为它创建类似于asp:label的id属性吗 谢谢 michaeld如果HtmlHelper函数不适合您的需要,则无需在任何地方始终使用它们。他们只是想让你的生活更轻松,而不是更艰难。在此处使用良好的ole HTML: <label id="id_for_label"></label> 如果您想继续使用HtmlHelper函数,您可以创建自己的扩展方法 例如: publ
michaeld如果HtmlHelper函数不适合您的需要,则无需在任何地方始终使用它们。他们只是想让你的生活更轻松,而不是更艰难。在此处使用良好的ole HTML:
<label id="id_for_label"></label>
如果您想继续使用HtmlHelper函数,您可以创建自己的扩展方法 例如:
public static class LabelHelper
{
private static string HtmlAttributes(object htmlAttributes)
{
var builder = new StringBuilder();
foreach (PropertyDescriptor descriptor in
TypeDescriptor.GetProperties(htmlAttributes))
{
builder.AppendFormat(" {0}=\"{1}\" ", descriptor.Name,
descriptor.GetValue(htmlAttributes));
}
return builder.ToString();
}
public static MvcHtmlString MyLabel(this HtmlHelper htmlHelper,
string labelText, object htmlAttributes)
{
var attributes = HtmlAttributes(htmlAttributes);
return MvcHtmlString.Create(
String.Format("<label for=\"{0}\" {1}>{0}</label",
labelText, attributes.Trim()));
}
}
公共静态类LabelHelper
{
私有静态字符串HtmlAttributes(对象HtmlAttributes)
{
var builder=新的StringBuilder();
foreach(中的PropertyDescriptor描述符)
TypeDescriptor.GetProperties(htmlAttributes))
{
AppendFormat(“{0}=\{1}\”,descriptor.Name,
descriptor.GetValue(htmlAttributes));
}
返回builder.ToString();
}
公共静态MvcHtmlString MyLabel(此HtmlHelper HtmlHelper,
字符串labelText,对象htmlAttributes)
{
var属性=HtmlAttributes(HtmlAttributes);
返回MvcHtmlString.Create(
String.Format(“{0}”
<%: Html.MyLabel("Hello, World!", new { @id = "myLabel" })%>
<label for="Hello, World!" id="myLabel">Hello, World!</label>