Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
@Html.DisplayName(), .DisplayText(), .Id(), .Name(), `.Value(…
Browse files Browse the repository at this point in the history
…)` return `string`

- fixes #566
- allows compositions such as `@Html.Label("property", Html.Id("property"))`
- adjust XML comments to match
- add missing XML comments to `HtmlHelperValueExtensions`
- correct typos in `DisplayText()` and `ValidationMessageFor()` comments
- also increase XML comment consistency for changed methods
  • Loading branch information
dougbu committed Jul 25, 2014
1 parent b0d52f7 commit 9f77e2c
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 72 deletions.
29 changes: 14 additions & 15 deletions src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,16 @@ public HtmlString DisplayForModel(string templateName,
}

/// <inheritdoc />
public HtmlString DisplayName(string expression)
public string DisplayName(string expression)
{
var metadata = ExpressionMetadataProvider.FromStringExpression(expression, ViewData, MetadataProvider);
return GenerateDisplayName(metadata, expression);
}

/// <inheritdoc />
public HtmlString DisplayText(string name)
public string DisplayText(string name)
{
var metadata = ExpressionMetadataProvider.FromStringExpression(name, ViewData, MetadataProvider);

return GenerateDisplayText(metadata);
}

Expand Down Expand Up @@ -305,7 +304,7 @@ public HtmlString Hidden(string name, object value, object htmlAttributes)
}

/// <inheritdoc />
public HtmlString Id(string name)
public string Id(string name)
{
return GenerateId(name);
}
Expand All @@ -328,10 +327,10 @@ public HtmlString ListBox(string name, IEnumerable<SelectListItem> selectList, o
}

/// <inheritdoc />
public virtual HtmlString Name(string name)
public virtual string Name(string name)
{
var fullName = ViewData.TemplateInfo.GetFullHtmlFieldName(name);
return new HtmlString(Encode(fullName));
return fullName;
}

/// <inheritdoc />
Expand Down Expand Up @@ -585,7 +584,7 @@ public HtmlString TextBox(string name, object value, string format, IDictionary<
}

/// <inheritdoc />
public HtmlString Value([NotNull] string name, string format)
public string Value([NotNull] string name, string format)
{
return GenerateValue(name, value: null, format: format, useViewData: true);
}
Expand Down Expand Up @@ -701,7 +700,7 @@ protected virtual HtmlString GenerateCheckBox(ModelMetadata metadata, string nam
htmlAttributes: htmlAttributeDictionary);
}

protected virtual HtmlString GenerateDisplayName([NotNull] ModelMetadata metadata, string htmlFieldName)
protected virtual string GenerateDisplayName([NotNull] ModelMetadata metadata, string htmlFieldName)
{
// We don't call ModelMetadata.GetDisplayName here because
// we want to fall back to the field name rather than the ModelType.
Expand All @@ -713,12 +712,12 @@ protected virtual HtmlString GenerateDisplayName([NotNull] ModelMetadata metadat
string.IsNullOrEmpty(htmlFieldName) ? string.Empty : htmlFieldName.Split('.').Last();
}

return new HtmlString(Encode(resolvedDisplayName));
return resolvedDisplayName;
}

protected virtual HtmlString GenerateDisplayText(ModelMetadata metadata)
protected virtual string GenerateDisplayText(ModelMetadata metadata)
{
return new HtmlString(metadata.SimpleDisplayText);
return metadata.SimpleDisplayText ?? string.Empty;
}

protected HtmlString GenerateDropDown(ModelMetadata metadata, string expression,
Expand Down Expand Up @@ -845,9 +844,9 @@ protected virtual HtmlString GenerateHidden(
htmlAttributes: htmlAttributeDictionary);
}

protected virtual HtmlString GenerateId(string expression)
protected virtual string GenerateId(string expression)
{
return new HtmlString(Encode(ViewData.TemplateInfo.GetFullHtmlFieldName(expression)));
return ViewData.TemplateInfo.GetFullHtmlFieldName(expression);
}

protected virtual HtmlString GenerateLabel([NotNull] ModelMetadata metadata,
Expand Down Expand Up @@ -1344,7 +1343,7 @@ protected virtual HtmlString GenerateValidationMessage(string expression,
return builder.ToHtmlString(TagRenderMode.Normal);
}

protected virtual HtmlString GenerateValue(string name, object value, string format, bool useViewData)
protected virtual string GenerateValue(string name, object value, string format, bool useViewData)
{
var fullName = ViewData.TemplateInfo.GetFullHtmlFieldName(name);
var attemptedValue = (string)GetModelStateValue(fullName, typeof(string));
Expand Down Expand Up @@ -1375,7 +1374,7 @@ protected virtual HtmlString GenerateValue(string name, object value, string for
resolvedValue = FormatValue(value, format);
}

return new HtmlString(Encode(resolvedValue));
return resolvedValue;
}

protected virtual IEnumerable<ModelClientValidationRule> GetClientValidationRules(
Expand Down
12 changes: 6 additions & 6 deletions src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelperOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ public HtmlString DisplayFor<TValue>([NotNull] Expression<Func<TModel, TValue>>
}

/// <inheritdoc />
public HtmlString DisplayNameFor<TValue>([NotNull] Expression<Func<TModel, TValue>> expression)
public string DisplayNameFor<TValue>([NotNull] Expression<Func<TModel, TValue>> expression)
{
var metadata = GetModelMetadata(expression);
return GenerateDisplayName(metadata, ExpressionHelper.GetExpressionText(expression));
}

/// <inheritdoc />
public HtmlString DisplayNameForInnerType<TInnerModel, TValue>(
public string DisplayNameForInnerType<TInnerModel, TValue>(
[NotNull] Expression<Func<TInnerModel, TValue>> expression)
{
var metadata = ExpressionMetadataProvider.FromLambdaExpression<TInnerModel, TValue>(
Expand All @@ -113,7 +113,7 @@ public HtmlString DisplayNameForInnerType<TInnerModel, TValue>(
}

/// <inheritdoc />
public HtmlString DisplayTextFor<TValue>([NotNull] Expression<Func<TModel, TValue>> expression)
public string DisplayTextFor<TValue>([NotNull] Expression<Func<TModel, TValue>> expression)
{
return GenerateDisplayText(GetModelMetadata(expression));
}
Expand Down Expand Up @@ -144,7 +144,7 @@ public HtmlString HiddenFor<TProperty>([NotNull] Expression<Func<TModel, TProper
}

/// <inheritdoc />
public HtmlString IdFor<TProperty>([NotNull] Expression<Func<TModel, TProperty>> expression)
public string IdFor<TProperty>([NotNull] Expression<Func<TModel, TProperty>> expression)
{
return GenerateId(GetExpressionName(expression));
}
Expand Down Expand Up @@ -172,7 +172,7 @@ public HtmlString ListBoxFor<TProperty>(
}

/// <inheritdoc />
public HtmlString NameFor<TProperty>([NotNull] Expression<Func<TModel, TProperty>> expression)
public string NameFor<TProperty>([NotNull] Expression<Func<TModel, TProperty>> expression)
{
var expressionName = GetExpressionName(expression);
return Name(expressionName);
Expand Down Expand Up @@ -242,7 +242,7 @@ public HtmlString ValidationMessageFor<TProperty>([NotNull] Expression<Func<TMod
}

/// <inheritdoc />
public HtmlString ValueFor<TProperty>(Expression<Func<TModel, TProperty>> expression, string format)
public string ValueFor<TProperty>(Expression<Func<TModel, TProperty>> expression, string format)
{
var metadata = GetModelMetadata(expression);
return GenerateValue(ExpressionHelper.GetExpressionText(expression), metadata.Model, format,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,19 @@ public static class HtmlHelperDisplayNameExtensions
/// <summary>
/// Gets the display name for the current model.
/// </summary>
/// <returns>An <see cref="HtmlString"/> that represents HTML markup.</returns>
public static HtmlString DisplayNameForModel([NotNull] this IHtmlHelper htmlHelper)
/// <returns>The display name.</returns>
public static string DisplayNameForModel([NotNull] this IHtmlHelper htmlHelper)
{
return htmlHelper.DisplayName(string.Empty);
}

/// <summary>
/// Gets the display name for the model.
/// Gets the display name.
/// </summary>
/// <param name="htmlHelper">The <see cref="IHtmlHelper{T}"/> instance that this method extends.</param>
/// <param name="expression">An expression that identifies the object that contains the display name.</param>
/// <returns>
/// The display name for the model.
/// </returns>
public static HtmlString DisplayNameFor<TInnerModel, TValue>(
/// <returns>The display name.</returns>
public static string DisplayNameFor<TInnerModel, TValue>(
[NotNull] this IHtmlHelper<IEnumerable<TInnerModel>> htmlHelper,
[NotNull] Expression<Func<TInnerModel, TValue>> expression)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ public static class HtmlHelperNameExtensions
/// Gets the full HTML field name for the current model.
/// </summary>
/// <param name="htmlHelper">The <see cref="HtmlHelper"/> instance that this method extends.</param>
/// <returns>An <see cref="HtmlString"/> that represents HTML markup.</returns>
public static HtmlString NameForModel([NotNull] this IHtmlHelper htmlHelper)
/// <returns>The name.</returns>
public static string NameForModel([NotNull] this IHtmlHelper htmlHelper)
{
return htmlHelper.Name(string.Empty);
}

/// <summary>
/// Gets the full HTML field id for the current model.
/// Gets the full HTML field Id for the current model.
/// </summary>
/// <param name="htmlHelper">The <see cref="HtmlHelper"/> instance that this method extends.</param>
/// <returns>An <see cref="HtmlString"/> that represents HTML markup.</returns>
public static HtmlString IdForModel([NotNull] this IHtmlHelper htmlHelper)
/// <returns>The Id of the HTML element.</returns>
public static string IdForModel([NotNull] this IHtmlHelper htmlHelper)
{
return htmlHelper.Id(string.Empty);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,47 @@ namespace Microsoft.AspNet.Mvc.Rendering
{
public static class HtmlHelperValueExtensions
{
public static HtmlString Value([NotNull] this IHtmlHelper htmlHelper, string name)
/// <summary>
/// Returns the model value for the given expression <paramref name="name"/>.
/// </summary>
/// <param name="htmlHelper">The <see cref="HtmlHelper"/> instance that this method extends.</param>
/// <param name="name">Name of an expression, relative to the current model.</param>
/// <returns>The value.</returns>
public static string Value([NotNull] this IHtmlHelper htmlHelper, string name)
{
return htmlHelper.Value(name, format: null);
}

public static HtmlString ValueFor<TModel, TProperty>(
/// <summary>
/// Returns the model value for the given <paramref name="expression"/>.
/// </summary>
/// <param name="htmlHelper">The <see cref="HtmlHelper"/> instance that this method extends.</param>
/// <param name="expression">An expression, relative to the current model.</param>
/// <returns>The value.</returns>
public static string ValueFor<TModel, TProperty>(
[NotNull] this IHtmlHelper<TModel> htmlHelper,
[NotNull] Expression<Func<TModel, TProperty>> expression)
{
return htmlHelper.ValueFor(expression, format: null);
}

public static HtmlString ValueForModel([NotNull] this IHtmlHelper htmlHelper)
/// <summary>
/// Returns the model value for the current model.
/// </summary>
/// <param name="htmlHelper">The <see cref="HtmlHelper"/> instance that this method extends.</param>
/// <returns>The value.</returns>
public static string ValueForModel([NotNull] this IHtmlHelper htmlHelper)
{
return ValueForModel(htmlHelper, format: null);
}

public static HtmlString ValueForModel([NotNull] this IHtmlHelper htmlHelper, string format)
/// <summary>
/// Returns the model value for the current model.
/// </summary>
/// <param name="htmlHelper">The <see cref="HtmlHelper"/> instance that this method extends.</param>
/// <param name="format">The optional format string to apply to the value.</param>
/// <returns>The value.</returns>
public static string ValueForModel([NotNull] this IHtmlHelper htmlHelper, string format)
{
return htmlHelper.Value(string.Empty, format);
}
Expand Down
31 changes: 15 additions & 16 deletions src/Microsoft.AspNet.Mvc.Core/Rendering/IHtmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,16 @@ HtmlString Display(
/// <returns>
/// The display name.
/// </returns>
HtmlString DisplayName(string expression);
string DisplayName(string expression);

/// Returns the HtmlString corresponding to the property in the model specified by the name.
/// <summary>
/// Gets the display text for the specified expression <paramref name="name"/>.
/// </summary>
/// <param name="name">
/// The string which identifies the object for which the HtmlString should be returned.</param>
/// <param name="name">Name of an expression, relative to the current model.</param>
/// <returns>
/// New <see cref="HtmlString"/> containing the display text. If the value is null,
/// then it returns the ModelMetadata.NullDisplayText.
/// The display text. If the value is null, returns <see cref="ModelMetadata.NullDisplayText"/>.
/// </returns>
HtmlString DisplayText(string name);
string DisplayText(string name);

/// <summary>
/// Returns a single-selection HTML {select} element using the specified name of the form field,
Expand Down Expand Up @@ -262,11 +261,11 @@ HtmlString DropDownList(
HtmlString Hidden(string name, object value, object htmlAttributes);

/// <summary>
/// Gets the Id of the given string.
/// Gets the HTML element Id for the specified expression <paramref name="name"/>.
/// </summary>
/// <param name="name">The string which identifies the object for which the Id should be returned.</param>
/// <returns>New <see cref="HtmlString"/> containing the Id.</returns>
HtmlString Id(string name);
/// <param name="name">Name of an expression, relative to the current model.</param>
/// <returns>The Id of the HTML element.</returns>
string Id(string name);

/// <summary>
/// Returns an HTML label element and the property name of the property that is represented by the specified
Expand Down Expand Up @@ -294,11 +293,11 @@ HtmlString DropDownList(
HtmlString ListBox(string name, IEnumerable<SelectListItem> selectList, object htmlAttributes);

/// <summary>
/// Gets the full HTML field name for the given expression <paramref name="name"/>.
/// Gets the full HTML element name for the given expression <paramref name="name"/>.
/// </summary>
/// <param name="name">Name of an expression, relative to the current model.</param>
/// <returns>An <see cref="HtmlString"/> that represents HTML markup.</returns>
HtmlString Name(string name);
/// <returns>The name.</returns>
string Name(string name);

/// <summary>
/// Returns a partial view in string format.
Expand Down Expand Up @@ -475,7 +474,7 @@ HtmlString ValidationSummary(
/// </summary>
/// <param name="name">Name of an expression, relative to the current model.</param>
/// <param name="format">The optional format string to apply to the value.</param>
/// <returns>An <see cref="HtmlString"/> that represents HTML markup.</returns>
HtmlString Value([NotNull] string name, string format);
/// <returns>The value.</returns>
string Value([NotNull] string name, string format);
}
}
Loading

0 comments on commit 9f77e2c

Please sign in to comment.