Addressed code review comments.

This commit is contained in:
N. Taylor Mullen 2014-04-02 16:15:14 -07:00
parent 5dbd006165
commit 2b3687282e
6 changed files with 76 additions and 38 deletions

View File

@ -1,10 +1,12 @@
@functions {
@using System.Globalization
@functions {
private bool? Value {
get {
if (ViewData.Model == null) {
return null;
}
return Convert.ToBoolean(ViewData.Model, System.Globalization.CultureInfo.InvariantCulture);
return Convert.ToBoolean(ViewData.Model, CultureInfo.InvariantCulture);
}
}
}

View File

@ -1,8 +1,10 @@
@functions {
@using System.Globalization
@functions {
private object FormattedValue {
get {
if (ViewData.TemplateInfo.FormattedModelValue == ViewData.ModelMetadata.Model) {
return String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0:0.00}", ViewData.ModelMetadata.Model);
return String.Format(CultureInfo.CurrentCulture, "{0:0.00}", ViewData.ModelMetadata.Model);
}
return ViewData.TemplateInfo.FormattedModelValue;
}

View File

@ -204,6 +204,12 @@
</form>
</div>
<div style="float: left; border: 5px solid red;">
@await Component.InvokeAsync("Tags", 15)
</div>
</div>
<div class="row">
<div style="float: left; border: 5px solid green;">
<table>
<tr>
@ -256,8 +262,4 @@
</tr>
</table>
</div>
<div style="float: right; border: 5px solid red;">
@await Component.InvokeAsync("Tags", 15)
</div>
</div>

View File

@ -162,22 +162,17 @@ namespace Microsoft.AspNet.Mvc.Rendering
return TagBuilder.CreateSanitizedId(name, IdAttributeDotReplacement);
}
public virtual HtmlString Display(string expression,
public HtmlString Display(string expression,
string templateName,
string htmlFieldName,
object additionalViewData)
{
var templateBuilder = new TemplateBuilder(ViewContext,
ViewData,
ExpressionMetadataProvider.FromStringExpression(expression, ViewData, MetadataProvider),
htmlFieldName ?? ExpressionHelper.GetExpressionText(expression),
templateName,
readOnly: true,
additionalViewData: additionalViewData);
var metadata = ExpressionMetadataProvider.FromStringExpression(expression, ViewData, MetadataProvider);
var templateResult = templateBuilder.Build();
return new HtmlString(templateResult);
return GenerateDisplay(metadata,
htmlFieldName ?? ExpressionHelper.GetExpressionText(expression),
templateName,
additionalViewData);
}
/// <inheritdoc />
@ -203,6 +198,25 @@ namespace Microsoft.AspNet.Mvc.Rendering
return RenderPartialCoreAsync(partialViewName, model, viewData, ViewContext.Writer);
}
protected virtual HtmlString GenerateDisplay(ModelMetadata metadata,
string htmlFieldName,
string templateName,
object additionalViewData)
{
var templateBuilder = new TemplateBuilder(_viewEngine,
ViewContext,
ViewData,
metadata,
templateName,
templateName,
readOnly: true,
additionalViewData: additionalViewData);
var templateResult = templateBuilder.Build();
return new HtmlString(templateResult);
}
protected virtual async Task RenderPartialCoreAsync([NotNull] string partialViewName,
object model,
ViewDataDictionary viewData,

View File

@ -1,29 +1,40 @@

namespace Microsoft.AspNet.Mvc.Rendering
{
public static class DisplayExtensions
public static class HtmlHelperDisplayExtensions
{
public static HtmlString Display<T>(this IHtmlHelper<T> html, string expression)
public static HtmlString Display<TModel>([NotNull] this IHtmlHelper<TModel> html,
string expression)
{
return html.Display(expression, templateName: null, htmlFieldName: null, additionalViewData: null);
}
public static HtmlString Display<T>(this IHtmlHelper<T> html, string expression, object additionalViewData)
public static HtmlString Display<TModel>([NotNull] this IHtmlHelper<TModel> html,
string expression,
object additionalViewData)
{
return html.Display(expression, templateName: null, htmlFieldName: null, additionalViewData: additionalViewData);
}
public static HtmlString Display<T>(this IHtmlHelper<T> html, string expression, string templateName)
public static HtmlString Display<TModel>([NotNull] this IHtmlHelper<TModel> html,
string expression,
string templateName)
{
return html.Display(expression, templateName, htmlFieldName: null, additionalViewData: null);
}
public static HtmlString Display<T>(this IHtmlHelper<T> html, string expression, string templateName, object additionalViewData)
public static HtmlString Display<TModel>([NotNull] this IHtmlHelper<TModel> html,
string expression,
string templateName,
object additionalViewData)
{
return html.Display(expression, templateName, htmlFieldName: null, additionalViewData: additionalViewData);
}
public static HtmlString Display<T>(this IHtmlHelper<T> html, string expression, string templateName, string htmlFieldName)
public static HtmlString Display<TModel>([NotNull] this IHtmlHelper<TModel> html,
string expression,
string templateName,
string htmlFieldName)
{
return html.Display(expression, templateName, htmlFieldName, additionalViewData: null);
}

View File

@ -11,19 +11,6 @@ namespace Microsoft.AspNet.Mvc.Rendering
/// <typeparam name="TModel">The <see cref="Type"/> of the model.</typeparam>
public interface IHtmlHelper<TModel>
{
/// <summary>
/// Returns HTML markup for each property in the object that is represented by the expression, using the specified template, HTML field ID, and additional view data.
/// </summary>
/// <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 Display(string expression,
string templateName,
string htmlFieldName,
object additionalViewData);
/// <summary>
/// Gets or sets the character that replaces periods in the ID attribute of an element.
/// </summary>
@ -44,6 +31,26 @@ namespace Microsoft.AspNet.Mvc.Rendering
/// </summary>
ViewDataDictionary<TModel> ViewData { get; }
/// <summary>
/// Returns HTML markup for each property in the object that is represented by the expression, using the specified
/// template, HTML field ID, and additional view data.
/// </summary>
/// <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 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 Display(string expression,
string templateName,
string htmlFieldName,
object additionalViewData);
/// <summary>
/// Converts the value of the specified object to an HTML-encoded string.
/// </summary>