Changes enabling TextArea, TextAreaFor.
Changing the interface to directly include Rows and Columns.
This commit is contained in:
parent
b899cac4c8
commit
112a5ddc50
|
|
@ -96,6 +96,7 @@ namespace MvcSample.Web
|
|||
Address = "Dependents address",
|
||||
Alive = false,
|
||||
},
|
||||
About = "I am a Software Engineer"
|
||||
};
|
||||
|
||||
return user;
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ namespace MvcSample.Web.Models
|
|||
public User Dependent { get; set; }
|
||||
public bool Alive { get; set; }
|
||||
public string Password { get; set; }
|
||||
|
||||
public string About { get; set; }
|
||||
public string Log { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -67,6 +67,14 @@
|
|||
@Html.DropDownListFor(model => model.Age, (IEnumerable<SelectListItem>)ViewBag.Ages, htmlAttributes: new { @class = "form-control" })
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<label class="control-label col-md-2">Model.Name</label>
|
||||
</td>
|
||||
<td>
|
||||
@Html.TextArea("About", "You can explain about your profession, hobbies etc.", 5, 40, htmlAttributes: new { style = "font-weight:bold" })
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<input type="submit" value="Save" class="btn btn-default" style="margin-left: 10px" />
|
||||
|
|
|
|||
|
|
@ -906,6 +906,22 @@ namespace Microsoft.AspNet.Mvc.Core
|
|||
return GetString("AuthorizeAttribute_OnAuthorizationNotImplemented");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The value must be greater than or equal to zero..
|
||||
/// </summary>
|
||||
internal static string HtmlHelper_TextAreaParameterOutOfRange
|
||||
{
|
||||
get { return GetString("HtmlHelper_TextAreaParameterOutOfRange"); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The value must be greater than or equal to zero..
|
||||
/// </summary>
|
||||
internal static string FormatHtmlHelper_TextAreaParameterOutOfRange()
|
||||
{
|
||||
return GetString("HtmlHelper_TextAreaParameterOutOfRange");
|
||||
}
|
||||
|
||||
private static string GetString(string name, params string[] formatterNames)
|
||||
{
|
||||
var value = _resourceManager.GetString(name);
|
||||
|
|
|
|||
|
|
@ -513,6 +513,18 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual HtmlString TextArea(string name, string value, int rows, int columns, object htmlAttributes)
|
||||
{
|
||||
var metadata = ExpressionMetadataProvider.FromStringExpression(name, ViewData, MetadataProvider);
|
||||
if (value != null)
|
||||
{
|
||||
metadata.Model = value;
|
||||
}
|
||||
|
||||
return GenerateTextArea(metadata, name, rows, columns, htmlAttributes);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public HtmlString TextBox(string name, object value, string format, IDictionary<string, object> htmlAttributes)
|
||||
{
|
||||
|
|
@ -959,48 +971,65 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
return tagBuilder.ToHtmlString(TagRenderMode.Normal);
|
||||
}
|
||||
|
||||
internal static MvcHtmlString TextAreaHelper(HtmlHelper htmlHelper, ModelMetadata modelMetadata, string name, IDictionary<string, object> rowsAndColumns, IDictionary<string, object> htmlAttributes, string innerHtmlPrefix = null)
|
||||
protected virtual HtmlString GenerateTextArea(ModelMetadata metadata, string name,
|
||||
int rows, int columns, object htmlAttributes)
|
||||
{
|
||||
string fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
|
||||
if (String.IsNullOrEmpty(fullName))
|
||||
if (rows < 0)
|
||||
{
|
||||
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");
|
||||
throw new ArgumentOutOfRangeException("rows", Resources.HtmlHelper_TextAreaParameterOutOfRange);
|
||||
}
|
||||
|
||||
TagBuilder tagBuilder = new TagBuilder("textarea");
|
||||
tagBuilder.GenerateId(fullName);
|
||||
tagBuilder.MergeAttributes(htmlAttributes, true);
|
||||
tagBuilder.MergeAttributes(rowsAndColumns, rowsAndColumns != implicitRowsAndColumns); // Only force explicit rows/cols
|
||||
tagBuilder.MergeAttribute("name", fullName, true);
|
||||
if (columns < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("columns", Resources.HtmlHelper_TextAreaParameterOutOfRange);
|
||||
}
|
||||
|
||||
var fullName = ViewData.TemplateInfo.GetFullHtmlFieldName(name);
|
||||
if (string.IsNullOrEmpty(fullName))
|
||||
{
|
||||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, "name");
|
||||
}
|
||||
|
||||
// If there are any errors for a named field, we add the CSS attribute.
|
||||
ModelState modelState;
|
||||
if (htmlHelper.ViewData.ModelState.TryGetValue(fullName, out modelState) && modelState.Errors.Count > 0)
|
||||
{
|
||||
tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
|
||||
}
|
||||
ViewData.ModelState.TryGetValue(fullName, out modelState);
|
||||
|
||||
tagBuilder.MergeAttributes(htmlHelper.GetUnobtrusiveValidationAttributes(name, modelMetadata));
|
||||
|
||||
string value;
|
||||
string value = string.Empty;
|
||||
if (modelState != null && modelState.Value != null)
|
||||
{
|
||||
value = modelState.Value.AttemptedValue;
|
||||
}
|
||||
else if (modelMetadata.Model != null)
|
||||
else if (metadata.Model != null)
|
||||
{
|
||||
value = modelMetadata.Model.ToString();
|
||||
value = metadata.Model.ToString();
|
||||
}
|
||||
else
|
||||
|
||||
var tagBuilder = new TagBuilder("textarea");
|
||||
tagBuilder.GenerateId(fullName, IdAttributeDotReplacement);
|
||||
tagBuilder.MergeAttributes(AnonymousObjectToHtmlAttributes(htmlAttributes), true);
|
||||
if (rows > 0)
|
||||
{
|
||||
value = String.Empty;
|
||||
tagBuilder.MergeAttribute("rows", rows.ToString(CultureInfo.InvariantCulture), true);
|
||||
}
|
||||
|
||||
if (columns > 0)
|
||||
{
|
||||
tagBuilder.MergeAttribute("columns", columns.ToString(CultureInfo.InvariantCulture), true);
|
||||
}
|
||||
|
||||
tagBuilder.MergeAttribute("name", fullName, true);
|
||||
tagBuilder.MergeAttributes(GetValidationAttributes(name, metadata));
|
||||
|
||||
// If there are any errors for a named field, we add this CSS attribute.
|
||||
if (modelState != null && modelState.Errors.Count > 0)
|
||||
{
|
||||
tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
|
||||
}
|
||||
|
||||
// The first newline is always trimmed when a TextArea is rendered, so we add an extra one
|
||||
// in case the value being rendered is something like "\r\nHello".
|
||||
tagBuilder.InnerHtml = (innerHtmlPrefix ?? Environment.NewLine) + HttpUtility.HtmlEncode(value);
|
||||
tagBuilder.InnerHtml = WebUtility.HtmlEncode(value);
|
||||
|
||||
return tagBuilder.ToMvcHtmlString(TagRenderMode.Normal);
|
||||
return tagBuilder.ToHtmlString(TagRenderMode.Normal);
|
||||
}
|
||||
|
||||
protected virtual HtmlString GenerateTextBox(ModelMetadata metadata, string name, object value, string format,
|
||||
|
|
|
|||
|
|
@ -166,6 +166,14 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
htmlAttributes: htmlAttributes);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public HtmlString TextAreaFor<TProperty>([NotNull] Expression<Func<TModel, TProperty>> expression, int rows,
|
||||
int columns, object htmlAttributes)
|
||||
{
|
||||
var metadata = GetModelMetadata(expression);
|
||||
return GenerateTextArea(metadata, GetExpressionName(expression), rows, columns, htmlAttributes);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public HtmlString TextBoxFor<TProperty>([NotNull] Expression<Func<TModel, TProperty>> expression,
|
||||
string format, IDictionary<string, object> htmlAttributes)
|
||||
|
|
|
|||
|
|
@ -184,5 +184,53 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
{
|
||||
return htmlHelper.TextBoxFor(expression, format: null, htmlAttributes: htmlAttributes);
|
||||
}
|
||||
|
||||
public static HtmlString TextArea([NotNull] this IHtmlHelper htmlHelper,
|
||||
string name)
|
||||
{
|
||||
return htmlHelper.TextArea(name, value: null, rows: 0, columns: 0, htmlAttributes: null);
|
||||
}
|
||||
|
||||
public static HtmlString TextArea([NotNull] this IHtmlHelper htmlHelper,
|
||||
string name, object htmlAttributes)
|
||||
{
|
||||
return htmlHelper.TextArea(name, value: null, rows: 0, columns:0, htmlAttributes: htmlAttributes);
|
||||
}
|
||||
|
||||
public static HtmlString TextArea([NotNull] this IHtmlHelper htmlHelper,
|
||||
string name, string value)
|
||||
{
|
||||
return htmlHelper.TextArea(name, value, rows: 0, columns: 0, htmlAttributes: null);
|
||||
}
|
||||
|
||||
public static HtmlString TextArea([NotNull] this IHtmlHelper htmlHelper,
|
||||
string name, string value, object htmlAttributes)
|
||||
{
|
||||
return htmlHelper.TextArea(name, value, rows: 0, columns: 0, htmlAttributes: htmlAttributes);
|
||||
}
|
||||
|
||||
public static HtmlString TextArea([NotNull] this IHtmlHelper htmlHelper,
|
||||
string name, string value, int rows, int columns, object htmlAttributes)
|
||||
{
|
||||
return htmlHelper.TextArea(name, value, rows, columns, htmlAttributes);
|
||||
}
|
||||
|
||||
public static HtmlString TextAreaFor<TModel, TProperty>([NotNull] this IHtmlHelper<TModel> htmlHelper,
|
||||
[NotNull] Expression<Func<TModel, TProperty>> expression)
|
||||
{
|
||||
return htmlHelper.TextAreaFor(expression, rows: 0, columns: 0, htmlAttributes: null);
|
||||
}
|
||||
|
||||
public static HtmlString TextAreaFor<TModel, TProperty>([NotNull] this IHtmlHelper<TModel> htmlHelper,
|
||||
[NotNull] Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
|
||||
{
|
||||
return htmlHelper.TextAreaFor(expression, rows: 0, columns: 0, htmlAttributes: htmlAttributes);
|
||||
}
|
||||
|
||||
public static HtmlString TextAreaFor<TModel, TProperty>([NotNull] this IHtmlHelper<TModel> htmlHelper,
|
||||
[NotNull] Expression<Func<TModel, TProperty>> expression, int rows, int columns, object htmlAttributes)
|
||||
{
|
||||
return htmlHelper.TextAreaFor(expression, rows, columns, htmlAttributes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -341,6 +341,24 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
/// <returns>A task that represents when rendering has completed.</returns>
|
||||
Task RenderPartialAsync([NotNull] string partialViewName, object model, ViewDataDictionary viewData);
|
||||
|
||||
/// <summary>
|
||||
/// Render a textarea.
|
||||
/// </summary>
|
||||
/// <param name="name">
|
||||
/// 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 <paramref name="value"/> is <c>null</c>.
|
||||
/// </param>
|
||||
/// <param name="value">
|
||||
/// If non-<c>null</c>, value to include in the element. Ignore if named value is found in submitted data.
|
||||
/// </param>
|
||||
/// <param name="rows">Number of rows in the textarea.</param>
|
||||
/// <param name="columns">Number of columns in the textarea.</param>
|
||||
/// <param name="htmlAttributes">
|
||||
/// <see cref="IDictionary{string, object}"/> containing additional HTML attributes.
|
||||
/// </param>
|
||||
/// <returns>New <see cref="HtmlString"/> containing the rendered HTML.</returns>
|
||||
HtmlString TextArea(string name, string value, int rows, int columns, object htmlAttributes);
|
||||
|
||||
/// <summary>
|
||||
/// Render an input element of type "text".
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -177,6 +177,19 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
HtmlString RadioButtonFor<TProperty>([NotNull] Expression<Func<TModel, TProperty>> expression, object value,
|
||||
object htmlAttributes);
|
||||
|
||||
/// <summary>
|
||||
/// Render a textarea.
|
||||
/// </summary>
|
||||
/// <param name="expression">An expression, relative to the current model.</param>
|
||||
/// <param name="rows">Number of rows in the textarea.</param>
|
||||
/// <param name="columns">Number of columns in the textarea.</param>
|
||||
/// <param name="htmlAttributes">
|
||||
/// <see cref="IDictionary{string, object}"/> containing additional HTML attributes.
|
||||
/// </param>
|
||||
/// <returns>New <see cref="HtmlString"/> containing the rendered HTML.</returns>
|
||||
HtmlString TextAreaFor<TProperty>([NotNull] Expression<Func<TModel, TProperty>> expression,
|
||||
int rows, int columns, object htmlAttributes);
|
||||
|
||||
/// <summary>
|
||||
/// Render an input element of type "text".
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -285,4 +285,7 @@
|
|||
<data name="AuthorizeAttribute_OnAuthorizationNotImplemented" xml:space="preserve">
|
||||
<value>OnAuthorization is not implemented by this filter, use OnAuthorizationAsync instead.</value>
|
||||
</data>
|
||||
</root>
|
||||
<data name="HtmlHelper_TextAreaParameterOutOfRange" xml:space="preserve">
|
||||
<value>The value must be greater than or equal to zero.</value>
|
||||
</data>
|
||||
</root>
|
||||
Loading…
Reference in New Issue