|
diff --git a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs
index 51e75dfa11..8a04566836 100644
--- a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs
+++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs
@@ -1,12 +1,12 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics.Contracts;
using System.Globalization;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
-using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.Mvc.Core;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.Rendering.Expressions;
@@ -50,8 +50,6 @@ namespace Microsoft.AspNet.Mvc.Rendering
public string IdAttributeDotReplacement { get; set; }
- public HttpContext HttpContext { get; private set; }
-
public ViewContext ViewContext
{
get
@@ -182,6 +180,11 @@ namespace Microsoft.AspNet.Mvc.Rendering
mvcForm.EndForm();
}
+ public HtmlString CheckBox(string name, bool? isChecked, object htmlAttributes)
+ {
+ return GenerateCheckBox(metadata: null, name: name, isChecked: isChecked, htmlAttributes: htmlAttributes);
+ }
+
public string Encode(string value)
{
return (!string.IsNullOrEmpty(value)) ? WebUtility.HtmlEncode(value) : string.Empty;
@@ -225,6 +228,12 @@ namespace Microsoft.AspNet.Mvc.Rendering
additionalViewData);
}
+ public HtmlString Hidden(string name, object value, object htmlAttributes)
+ {
+ return GenerateHidden(metadata: null, name: name, value: value, useViewData: (value == null),
+ htmlAttributes: htmlAttributes);
+ }
+
public virtual HtmlString Name(string name)
{
var fullName = ViewData.TemplateInfo.GetFullHtmlFieldName(name);
@@ -287,6 +296,17 @@ namespace Microsoft.AspNet.Mvc.Rendering
await viewEngineResult.View.RenderAsync(newViewContext);
}
+ public HtmlString Password(string name, object value, object htmlAttributes)
+ {
+ return GeneratePassword(metadata: null, name: name, value: value, htmlAttributes: htmlAttributes);
+ }
+
+ public HtmlString RadioButton(string name, object value, bool? isChecked, object htmlAttributes)
+ {
+ return GenerateRadioButton(metadata: null, name: name, value: value, isChecked: isChecked,
+ htmlAttributes: htmlAttributes);
+ }
+
public virtual HtmlString ValidationSummary(bool excludePropertyErrors, string message, IDictionary htmlAttributes)
{
var formContext = ViewContext.ClientValidationEnabled ? ViewContext.FormContext : null;
@@ -417,6 +437,16 @@ namespace Microsoft.AspNet.Mvc.Rendering
return new MvcForm(ViewContext);
}
+ protected bool EvalBoolean(string key)
+ {
+ return Convert.ToBoolean(ViewData.Eval(key), CultureInfo.InvariantCulture);
+ }
+
+ protected string EvalString(string key)
+ {
+ return Convert.ToString(ViewData.Eval(key), CultureInfo.CurrentCulture);
+ }
+
protected string EvalString(string key, string format)
{
return Convert.ToString(ViewData.Eval(key, format), CultureInfo.CurrentCulture);
@@ -447,6 +477,54 @@ namespace Microsoft.AspNet.Mvc.Rendering
return new Dictionary();
}
+ protected virtual HtmlString GenerateCheckBox(ModelMetadata metadata, string name, bool? isChecked,
+ object htmlAttributes)
+ {
+ if (metadata != null)
+ {
+ // CheckBoxFor() case. That API does not support passing isChecked directly.
+ Contract.Assert(!isChecked.HasValue);
+
+ if (metadata.Model != null)
+ {
+ bool modelChecked;
+ if (Boolean.TryParse(metadata.Model.ToString(), out modelChecked))
+ {
+ isChecked = modelChecked;
+ }
+ }
+ }
+
+ // Only need a dictionary if htmlAttributes is non-null. TagBuilder.MergeAttributes() is fine with null.
+ IDictionary htmlAttributeDictionary = null;
+ if (htmlAttributes != null)
+ {
+ htmlAttributeDictionary = htmlAttributes as IDictionary;
+ if (htmlAttributeDictionary == null)
+ {
+ htmlAttributeDictionary = AnonymousObjectToHtmlAttributes(htmlAttributes);
+ }
+ }
+
+ var explicitValue = isChecked.HasValue;
+ if (explicitValue && htmlAttributeDictionary != null)
+ {
+ // Explicit value must override dictionary
+ htmlAttributeDictionary.Remove("checked");
+ }
+
+ return GenerateInput(InputType.CheckBox,
+ metadata,
+ name,
+ value: "true",
+ useViewData: !explicitValue,
+ isChecked: isChecked ?? false,
+ setId: true,
+ isExplicitValue: false,
+ format: null,
+ htmlAttributes: htmlAttributeDictionary);
+ }
+
///
/// Writes an opening
void EndForm();
+ ///
+ /// Render an input element of type "checkbox" with value "true" and an input element of type "hidden" with
+ /// value "false".
+ ///
+ ///
+ /// Rendered element's name. Also use this name to find value in submitted data or view data. Use view data
+ /// only if value is not in submitted data and is null.
+ ///
+ ///
+ /// If true, checkbox is initially checked. Ignore if named value is found in submitted data. Finally
+ /// fall back to an existing "checked" value in .
+ ///
+ /// An object that contains the HTML attributes to set for the element.
+ /// Alternatively, an instance containing the HTML attributes.
+ ///
+ /// New containing the rendered HTML.
+ HtmlString CheckBox(string name, bool? isChecked, object htmlAttributes);
+
+ ///
+ /// Render an input element of type "checkbox" with value "true" and an input element of type "hidden" with
+ /// value "false".
+ ///
+ ///
+ /// An expression that identifies the object that contains the properties to render.
+ ///
+ /// An object that contains the HTML attributes to set for the element.
+ /// Alternatively, an instance containing the HTML attributes.
+ ///
+ /// New containing the rendered HTML.
+ HtmlString CheckBoxFor([NotNull] Expression> expression, object htmlAttributes);
+
///
/// 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.
@@ -158,6 +189,14 @@ namespace Microsoft.AspNet.Mvc.Rendering
/// The HTML-encoded string.
string Encode(string value);
+ ///
+ /// Formats the value.
+ ///
+ /// The value.
+ /// The format string.
+ /// The formatted value.
+ string FormatValue(object value, string format);
+
///
/// Creates an HTML element ID using the specified element name.
///
@@ -165,6 +204,35 @@ namespace Microsoft.AspNet.Mvc.Rendering
/// The ID of the HTML element.
string GenerateIdFromName(string name);
+ ///
+ /// Render an input element of type "hidden".
+ ///
+ ///
+ /// Rendered element's name. Also use this name to find value in submitted data or view data. Use view data
+ /// only if value is not in submitted data and is null.
+ ///
+ ///
+ /// If non-null, value to include in the element. Ignore if named value is found in submitted data.
+ ///
+ /// An object that contains the HTML attributes to set for the element.
+ /// Alternatively, an instance containing the HTML attributes.
+ ///
+ /// New containing the rendered HTML.
+ HtmlString Hidden(string name, object value, object htmlAttributes);
+
+ ///
+ /// Render an input element of type "hidden".
+ ///
+ ///
+ /// An expression that identifies the object that contains the properties to render.
+ ///
+ /// An object that contains the HTML attributes to set for the element.
+ /// Alternatively, an instance containing the HTML attributes.
+ ///
+ /// New containing the rendered HTML.
+ HtmlString HiddenFor([NotNull] Expression> expression,
+ object htmlAttributes);
+
///
/// Gets the full HTML field name for the given expression .
///
@@ -180,6 +248,76 @@ namespace Microsoft.AspNet.Mvc.Rendering
/// An that represents HTML markup.
HtmlString NameFor([NotNull] Expression> expression);
+ ///
+ /// Render an input element of type "password".
+ ///
+ ///
+ /// Rendered element's name. Also use this name to find value in view data. Use view data
+ /// only if value is not in submitted data and is null.
+ ///
+ ///
+ /// If non-null, value to include in the element.
+ ///
+ /// An object that contains the HTML attributes to set for the element.
+ /// Alternatively, an instance containing the HTML attributes.
+ ///
+ /// New containing the rendered HTML.
+ HtmlString Password(string name, object value, object htmlAttributes);
+
+ ///
+ /// Render an input element of type "password".
+ ///
+ ///
+ /// An expression that identifies the object that contains the properties to render.
+ ///
+ /// An object that contains the HTML attributes to set for the element.
+ /// Alternatively, an instance containing the HTML attributes.
+ ///
+ /// New containing the rendered HTML.
+ HtmlString PasswordFor([NotNull] Expression> expression,
+ object htmlAttributes);
+
+ ///
+ /// Render an input element of type "radio".
+ ///
+ ///
+ /// Rendered element's name. Also use this name to find value in submitted data or view data. Use view data
+ /// only if value is not in submitted data and is null.
+ ///
+ ///
+ /// If non-null, value to include in the element. May be null only if
+ /// is also null. Also compared to value in submitted data or view data to
+ /// determine if that parameter is null. Ignore if named value is found in
+ /// submitted data.
+ ///
+ ///
+ /// If true, radio button is initially selected. Ignore if named value is found in submitted data. Fall
+ /// back to comparing with view data if this parameter is null. Finally
+ /// fall back to an existing "checked" value in .
+ ///
+ /// An object that contains the HTML attributes to set for the element.
+ /// Alternatively, an instance containing the HTML attributes.
+ ///
+ /// New containing the rendered HTML.
+ HtmlString RadioButton(string name, object value, bool? isChecked, object htmlAttributes);
+
+ ///
+ /// Render an input element of type "radio".
+ ///
+ ///
+ /// An expression that identifies the object that contains the properties to render.
+ ///
+ ///
+ /// If non-null, value to compare with current expression value to determine whether radio button is
+ /// checked.
+ ///
+ /// An object that contains the HTML attributes to set for the element.
+ /// Alternatively, an instance containing the HTML attributes.
+ ///
+ /// New containing the rendered HTML.
+ HtmlString RadioButtonFor([NotNull] Expression> expression, object value,
+ object htmlAttributes);
+
///
/// Wraps HTML markup in an , which will enable HTML markup to be
/// rendered to the output without getting HTML encoded.
|