Implement DisplayFor Html Helper.

Essentially add extension methods and call through to infrastructure.
This commit is contained in:
N. Taylor Mullen 2014-04-02 15:25:45 -07:00
parent 2b3687282e
commit 52ce83db9e
3 changed files with 72 additions and 0 deletions

View File

@ -175,6 +175,24 @@ 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)
{

View File

@ -0,0 +1,39 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
namespace Microsoft.AspNet.Mvc.Rendering
{
public static class DisplayForExtensions
{
[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)
{
return html.DisplayFor<TModel, 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)
{
return html.DisplayFor<TModel, 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)
{
return html.DisplayFor<TModel, 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)
{
return html.DisplayFor<TModel, 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)
{
return html.DisplayFor<TModel, TValue>(expression, templateName: templateName, htmlFieldName: htmlFieldName, additionalViewData: null);
}
}
}

View File

@ -51,6 +51,21 @@ namespace Microsoft.AspNet.Mvc.Rendering
string htmlFieldName,
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.
/// </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>
/// <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);
/// <summary>
/// Converts the value of the specified object to an HTML-encoded string.
/// </summary>