Adding Value Html Helpers

This commit is contained in:
Ryan Nowak 2014-04-01 13:24:22 -07:00
parent 42561f14d3
commit 49de9d2828
5 changed files with 105 additions and 1 deletions

View File

@ -71,6 +71,9 @@
<td>
'@Html.NameForModel()'
</td>
<td>
'@Html.ValueForModel()'
</td>
</tr>
<tr>
<td>
@ -79,6 +82,9 @@
<td>
'@Html.Name("Name")'
</td>
<td>
'@Html.Value("Name")'
</td>
</tr>
<tr>
<td>
@ -87,6 +93,9 @@
<td>
'@Html.NameFor(m => m.Address)'
</td>
<td>
'@Html.ValueFor(m => m.Address)'
</td>
</tr>
<tr>
<td>
@ -95,6 +104,9 @@
<td>
'@Html.Name("Anon.Address.Street")'
</td>
<td>
'@Html.Value("Anon.Address.Street")'
</td>
</tr>
</table>
</div>

View File

@ -8,6 +8,7 @@ using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.Rendering.Expressions;
namespace Microsoft.AspNet.Mvc.Rendering
{
@ -300,6 +301,11 @@ namespace Microsoft.AspNet.Mvc.Rendering
htmlAttributes: htmlAttributes);
}
public HtmlString Value([NotNull] string name, string format)
{
return GenerateValue(name, value: null, format: format, useViewData: true);
}
protected string EvalString(string key, string format)
{
return Convert.ToString(ViewData.Eval(key, format), CultureInfo.CurrentCulture);
@ -394,6 +400,42 @@ namespace Microsoft.AspNet.Mvc.Rendering
return tagBuilder.ToHtmlString(TagRenderMode.SelfClosing);
}
protected virtual HtmlString GenerateValue(string name, object value, string format, bool useViewData)
{
var fullName = ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
var attemptedValue = (string)GetModelStateValue(fullName, typeof(string));
string resolvedValue;
if (attemptedValue != null)
{
// case 1: if ModelState has a value then it's already formatted so ignore format string
resolvedValue = attemptedValue;
}
else if (useViewData)
{
if (name.Length == 0)
{
// case 2(a): format the value from ModelMetadata for the current model
var metadata = ViewData.ModelMetadata;
resolvedValue = FormatValue(metadata.Model, format);
}
else
{
// case 2(b): format the value from ViewData
resolvedValue = EvalString(name, format);
}
}
else
{
// case 3: format the explicit value from ModelMetadata
resolvedValue = FormatValue(value, format);
}
return new HtmlString(Encode(resolvedValue));
}
private static string GetInputTypeString(InputType inputType)
{
switch (inputType)

View File

@ -72,5 +72,11 @@ namespace Microsoft.AspNet.Mvc.Rendering
return metadata;
}
public HtmlString ValueFor<TProperty>(Expression<Func<TModel, TProperty>> expression, string format)
{
var metadata = GetModelMetadata(expression);
return GenerateValue(ExpressionHelper.GetExpressionText(expression), metadata.Model, format, useViewData: false);
}
}
}

View File

@ -0,0 +1,28 @@
using System;
using System.Linq.Expressions;
namespace Microsoft.AspNet.Mvc.Rendering
{
public static class HtmlHelperValueExtensions
{
public static HtmlString Value<TModel>([NotNull] this IHtmlHelper<TModel> htmlHelper, string name)
{
return htmlHelper.Value(name, format: null);
}
public static HtmlString ValueFor<TModel, TProperty>([NotNull] this IHtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
return htmlHelper.ValueFor(expression, format: null);
}
public static HtmlString ValueForModel<TModel>([NotNull] this IHtmlHelper<TModel> htmlHelper)
{
return ValueForModel<TModel>(htmlHelper, format: null);
}
public static HtmlString ValueForModel<TModel>([NotNull] this IHtmlHelper<TModel> htmlHelper, string format)
{
return htmlHelper.Value(string.Empty, format);
}
}
}

View File

@ -130,7 +130,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
/// </param>
/// <returns>New <see cref="HtmlString"/> containing the rendered HTML.</returns>
HtmlString TextBoxFor<TProperty>([NotNull] Expression<Func<TModel, TProperty>> expression, string format,
IDictionary<string, object> htmlAttributes);
IDictionary<string, object> htmlAttributes);
/// <summary>
/// Returns an unordered list (ul element) of validation messages that are in the
@ -144,5 +144,21 @@ namespace Microsoft.AspNet.Mvc.Rendering
/// </returns>
HtmlString ValidationSummary(bool excludePropertyErrors, string message,
IDictionary<string, object> htmlAttributes);
/// <summary>
/// Returns the model value for the given expression <paramref name="name"/>.
/// </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);
/// <summary>
/// Returns the model value for the given expression <paramref name="expression"/>.
/// </summary>
/// <param name="expression">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 ValueFor<TProperty>([NotNull] Expression<Func<TModel, TProperty>> expression, string format);
}
}