0
我想使用Asp的Html.LabelFor()
擴展名。 Net MVC 4將html屬性添加到Label
。我正在使用MVC 3。我的問題是:我可以簡單升級我的dll?(這有什麼問題?...)或者它會在部署中出現問題?在MVC 3中使用MVC 4擴展3
我想使用Asp的Html.LabelFor()
擴展名。 Net MVC 4將html屬性添加到Label
。我正在使用MVC 3。我的問題是:我可以簡單升級我的dll?(這有什麼問題?...)或者它會在部署中出現問題?在MVC 3中使用MVC 4擴展3
這是MVC3源代碼
internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string labelText = null) {
string resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
if (String.IsNullOrEmpty(resolvedLabelText)) {
return MvcHtmlString.Empty;
}
TagBuilder tag = new TagBuilder("label");
tag.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)));
tag.SetInnerText(resolvedLabelText);
return tag.ToMvcHtmlString(TagRenderMode.Normal);}
,這是mvc4
internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string labelText = null, IDictionary<string, object> htmlAttributes = null){
string resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
if (String.IsNullOrEmpty(resolvedLabelText))
{
return MvcHtmlString.Empty;
}
TagBuilder tag = new TagBuilder("label");
tag.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)));
tag.SetInnerText(resolvedLabelText);
tag.MergeAttributes(htmlAttributes, replaceExisting: true);
return tag.ToMvcHtmlString(TagRenderMode.Normal);}
只有一個行MVC3 InputExtensions.cs diffrenct tag.MergeAttributes(htmlAttributes, replaceExisting: true);
你可以找到TagBuilder.MergeAttributes函數用過的。
所以你可以自己寫一個擴展方法,而不需要升級你的項目。
下面是mvc4中的所有代碼,也許它會幫助你
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
namespace System.Web.Mvc.Html
{
public static class LabelExtensions
{
public static MvcHtmlString Label(this HtmlHelper html, string expression)
{
return Label(html,
expression,
labelText: null);
}
public static MvcHtmlString Label(this HtmlHelper html, string expression, string labelText)
{
return Label(html, expression, labelText, htmlAttributes: null, metadataProvider: null);
}
public static MvcHtmlString Label(this HtmlHelper html, string expression, object htmlAttributes)
{
return Label(html, expression, labelText: null, htmlAttributes: htmlAttributes, metadataProvider: null);
}
public static MvcHtmlString Label(this HtmlHelper html, string expression, IDictionary<string, object> htmlAttributes)
{
return Label(html, expression, labelText: null, htmlAttributes: htmlAttributes, metadataProvider: null);
}
public static MvcHtmlString Label(this HtmlHelper html, string expression, string labelText, object htmlAttributes)
{
return Label(html, expression, labelText, htmlAttributes, metadataProvider: null);
}
public static MvcHtmlString Label(this HtmlHelper html, string expression, string labelText, IDictionary<string, object> htmlAttributes)
{
return Label(html, expression, labelText, htmlAttributes, metadataProvider: null);
}
internal static MvcHtmlString Label(this HtmlHelper html, string expression, string labelText, object htmlAttributes, ModelMetadataProvider metadataProvider)
{
return Label(html,
expression,
labelText,
HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes),
metadataProvider);
}
internal static MvcHtmlString Label(this HtmlHelper html, string expression, string labelText, IDictionary<string, object> htmlAttributes, ModelMetadataProvider metadataProvider)
{
return LabelHelper(html,
ModelMetadata.FromStringExpression(expression, html.ViewData, metadataProvider),
expression,
labelText,
htmlAttributes);
}
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
return LabelFor<TModel, TValue>(html, expression, labelText: null);
}
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText)
{
return LabelFor(html, expression, labelText, htmlAttributes: null, metadataProvider: null);
}
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
{
return LabelFor(html, expression, labelText: null, htmlAttributes: htmlAttributes, metadataProvider: null);
}
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes)
{
return LabelFor(html, expression, labelText: null, htmlAttributes: htmlAttributes, metadataProvider: null);
}
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText, object htmlAttributes)
{
return LabelFor(html, expression, labelText, htmlAttributes, metadataProvider: null);
}
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText, IDictionary<string, object> htmlAttributes)
{
return LabelFor(html, expression, labelText, htmlAttributes, metadataProvider: null);
}
internal static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText, object htmlAttributes, ModelMetadataProvider metadataProvider)
{
return LabelFor(html,
expression,
labelText,
HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes),
metadataProvider);
}
internal static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText, IDictionary<string, object> htmlAttributes, ModelMetadataProvider metadataProvider)
{
return LabelHelper(html,
ModelMetadata.FromLambdaExpression(expression, html.ViewData, metadataProvider),
ExpressionHelper.GetExpressionText(expression),
labelText,
htmlAttributes);
}
public static MvcHtmlString LabelForModel(this HtmlHelper html)
{
return LabelForModel(html, labelText: null);
}
public static MvcHtmlString LabelForModel(this HtmlHelper html, string labelText)
{
return LabelHelper(html, html.ViewData.ModelMetadata, String.Empty, labelText);
}
public static MvcHtmlString LabelForModel(this HtmlHelper html, object htmlAttributes)
{
return LabelHelper(html, html.ViewData.ModelMetadata, String.Empty, labelText: null, htmlAttributes: HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString LabelForModel(this HtmlHelper html, IDictionary<string, object> htmlAttributes)
{
return LabelHelper(html, html.ViewData.ModelMetadata, String.Empty, labelText: null, htmlAttributes: htmlAttributes);
}
public static MvcHtmlString LabelForModel(this HtmlHelper html, string labelText, object htmlAttributes)
{
return LabelHelper(html, html.ViewData.ModelMetadata, String.Empty, labelText, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString LabelForModel(this HtmlHelper html, string labelText, IDictionary<string, object> htmlAttributes)
{
return LabelHelper(html, html.ViewData.ModelMetadata, String.Empty, labelText, htmlAttributes);
}
internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string labelText = null, IDictionary<string, object> htmlAttributes = null)
{
string resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
if (String.IsNullOrEmpty(resolvedLabelText))
{
return MvcHtmlString.Empty;
}
TagBuilder tag = new TagBuilder("label");
tag.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)));
tag.SetInnerText(resolvedLabelText);
tag.MergeAttributes(htmlAttributes, replaceExisting: true);
return tag.ToMvcHtmlString(TagRenderMode.Normal);
}
}
}