Addressed code review comments

This commit is contained in:
N. Taylor Mullen 2014-04-03 10:54:10 -07:00
parent 94bae850c7
commit 4f67dee048
4 changed files with 55 additions and 44 deletions

View File

@ -175,24 +175,6 @@ namespace Microsoft.AspNet.Mvc.Rendering
additionalViewData);
}
public virtual HtmlString DisplayFor<TModel, TValue>(Expression<Func<TModel, TValue>> expression,
string templateName,
string htmlFieldName,
object additionalViewData)
{
var templateBuilder = new TemplateBuilder(ViewContext,
ViewData,
ExpressionMetadataProvider.FromLambdaExpression(expression, (ViewDataDictionary<TModel>)ViewData, MetadataProvider),
htmlFieldName ?? ExpressionHelper.GetExpressionText(expression),
templateName,
readOnly: true,
additionalViewData: additionalViewData);
var templateResult = templateBuilder.Build();
return new HtmlString(templateResult);
}
/// <inheritdoc />
public virtual HtmlString Name(string name)
{
@ -217,9 +199,9 @@ namespace Microsoft.AspNet.Mvc.Rendering
}
protected virtual HtmlString GenerateDisplay(ModelMetadata metadata,
string htmlFieldName,
string templateName,
object additionalViewData)
string htmlFieldName,
string templateName,
object additionalViewData)
{
var templateBuilder = new TemplateBuilder(_viewEngine,
ViewContext,

View File

@ -41,6 +41,22 @@ namespace Microsoft.AspNet.Mvc.Rendering
base.Contextualize(viewContext);
}
public HtmlString DisplayFor<TValue>([NotNull] Expression<Func<TModel, TValue>> expression,
string templateName,
string htmlFieldName,
object additionalViewData)
{
var metadata = ExpressionMetadataProvider.FromLambdaExpression(expression,
ViewData,
MetadataProvider);
return GenerateDisplay(metadata,
htmlFieldName ?? ExpressionHelper.GetExpressionText(expression),
templateName,
additionalViewData);
}
/// <inheritdoc />
public HtmlString NameFor<TProperty>([NotNull] Expression<Func<TModel, TProperty>> expression)
{

View File

@ -4,36 +4,42 @@ using System.Linq.Expressions;
namespace Microsoft.AspNet.Mvc.Rendering
{
public static class DisplayForExtensions
public static class HtmlHelperDisplayForExtensions
{
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static HtmlString DisplayFor<TModel, TValue>(this IHtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
public static HtmlString DisplayFor<TModel, TValue>([NotNull] this IHtmlHelper<TModel> html,
[NotNull] Expression<Func<TModel, TValue>> expression)
{
return html.DisplayFor<TModel, TValue>(expression, templateName: null, htmlFieldName: null, additionalViewData: null);
return html.DisplayFor<TValue>(expression, templateName: null, htmlFieldName: null, additionalViewData: null);
}
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static HtmlString DisplayFor<TModel, TValue>(this IHtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object additionalViewData)
public static HtmlString DisplayFor<TModel, TValue>([NotNull] this IHtmlHelper<TModel> html,
[NotNull] Expression<Func<TModel, TValue>> expression,
object additionalViewData)
{
return html.DisplayFor<TModel, TValue>(expression, templateName: null, htmlFieldName: null, additionalViewData: additionalViewData);
return html.DisplayFor<TValue>(expression, templateName: null, htmlFieldName: null, additionalViewData: additionalViewData);
}
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static HtmlString DisplayFor<TModel, TValue>(this IHtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string templateName)
public static HtmlString DisplayFor<TModel, TValue>([NotNull] this IHtmlHelper<TModel> html,
[NotNull] Expression<Func<TModel, TValue>> expression,
string templateName)
{
return html.DisplayFor<TModel, TValue>(expression, templateName, htmlFieldName: null, additionalViewData: null);
return html.DisplayFor<TValue>(expression, templateName, htmlFieldName: null, additionalViewData: null);
}
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static HtmlString DisplayFor<TModel, TValue>(this IHtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string templateName, object additionalViewData)
public static HtmlString DisplayFor<TModel, TValue>([NotNull] this IHtmlHelper<TModel> html,
[NotNull] Expression<Func<TModel, TValue>> expression,
string templateName,
object additionalViewData)
{
return html.DisplayFor<TModel, TValue>(expression, templateName, htmlFieldName: null, additionalViewData: additionalViewData);
return html.DisplayFor<TValue>(expression, templateName, htmlFieldName: null, additionalViewData: additionalViewData);
}
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
public static HtmlString DisplayFor<TModel, TValue>(this IHtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string templateName, string htmlFieldName)
public static HtmlString DisplayFor<TModel, TValue>([NotNull] this IHtmlHelper<TModel> html,
[NotNull] Expression<Func<TModel, TValue>> expression,
string templateName,
string htmlFieldName)
{
return html.DisplayFor<TModel, TValue>(expression, templateName: templateName, htmlFieldName: htmlFieldName, additionalViewData: null);
return html.DisplayFor<TValue>(expression, templateName: templateName, htmlFieldName: htmlFieldName, additionalViewData: null);
}
}
}

View File

@ -52,19 +52,26 @@ namespace Microsoft.AspNet.Mvc.Rendering
object additionalViewData);
/// <summary>
/// Returns HTML markup for each property in the object that is represented by the specified expression, using the template, an HTML field ID, and additional view data.
/// Returns HTML markup for each property in the object that is represented by the specified expression, using the
/// template, an HTML field ID, and additional view data.
/// </summary>
/// <typeparam name="TDisplayModel">The type of the model.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="expression">An expression that identifies the object that contains the properties to display.</param>
/// <param name="templateName">The name of the template that is used to render the object.</param>
/// <param name="htmlFieldName">A string that is used to disambiguate the names of HTML input elements that are rendered for properties that have the same name.</param>
/// <param name="additionalViewData">An anonymous object that can contain additional view data that will be merged into the <see cref="ViewDataDictionary{TModel}"/> instance that is created for the template.</param>
/// <param name="htmlFieldName">
/// A string that is used to disambiguate the names of HTML input elements that are rendered for properties that have
/// the same name.
/// </param>
/// <param name="additionalViewData">
/// An anonymous object or dictionary that can contain additional view data that will be merged into the
/// <see cref="ViewDataDictionary{TModel}"/> instance that is created for the template.
/// </param>
/// <returns>The HTML markup for each property in the object that is represented by the expression.</returns>
HtmlString DisplayFor<TDisplayModel, TValue>(Expression<Func<TDisplayModel, TValue>> expression,
string templateName,
string htmlFieldName,
object additionalViewData);
HtmlString DisplayFor<TValue>(Expression<Func<TModel, TValue>> expression,
string templateName,
string htmlFieldName,
object additionalViewData);
/// <summary>
/// Converts the value of the specified object to an HTML-encoded string.