Если вы хотите продолжать использовать функции 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()));
}
}
Затем вы можете добавить метку к представлению в следующемобразом:
<%: Html.MyLabel("Hello, World!", new { @id = "myLabel" })%>
Сгенерированный HTML:
<label for="Hello, World!" id="myLabel">Hello, World!</label>
Для MVC 3 такая вспомогательная функция уже доступна:
http://msdn.microsoft.com/en-us/library/gg538318(v=VS.99).aspx