// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Html;
namespace Microsoft.AspNetCore.Mvc.ViewFeatures
{
///
/// Information about the current <form>.
///
///
/// Literal <form> elements in a view will share the default instance unless tag
/// helpers are enabled.
///
public class FormContext
{
private Dictionary _renderedFields;
private Dictionary _formData;
private IList _endOfFormContent;
///
/// Gets a property bag for any information you wish to associate with a <form/> in an
/// implementation or extension method.
///
public IDictionary FormData
{
get
{
if (_formData == null)
{
_formData = new Dictionary(StringComparer.Ordinal);
}
return _formData;
}
}
///
/// Gets or sets an indication the current <form> element contains an antiforgery token. Do not use
/// unless is true.
///
///
/// true if the current <form> element contains an antiforgery token; false otherwise.
///
public bool HasAntiforgeryToken { get; set; }
///
/// Gets an indication the property bag has been used and likely contains entries.
///
///
/// true if the backing field for is non-null; false otherwise.
///
public bool HasFormData => _formData != null;
///
/// Gets an indication the collection has been used and likely contains entries.
///
///
/// true if the backing field for is non-null; false
/// otherwise.
///
public bool HasEndOfFormContent => _endOfFormContent != null;
///
/// Gets an collection that should be rendered just prior to the next </form>
/// end tag. Do not use unless is true.
///
public IList EndOfFormContent
{
get
{
if (_endOfFormContent == null)
{
_endOfFormContent = new List();
}
return _endOfFormContent;
}
}
///
/// Gets or sets an indication whether extra content can be rendered at the end of the content of this
/// <form> element. That is, will be rendered just prior to the
/// </form> end tag.
///
///
/// true if the framework will render ; false otherwise. In
/// particular, true if the current <form> is associated with a tag helper or will be generated by
/// an HTML helper; false when using the default instance.
///
public bool CanRenderAtEndOfForm { get; set; }
///
/// Gets a dictionary mapping full HTML field names to indications that the named field has been rendered in
/// this <form>.
///
private Dictionary RenderedFields
{
get
{
if (_renderedFields == null)
{
_renderedFields = new Dictionary(StringComparer.Ordinal);
}
return _renderedFields;
}
}
///
/// Returns an indication based on that the given has
/// been rendered in this <form>.
///
/// The full HTML name of a field that may have been rendered.
///
/// true if the given has been rendered; false otherwise.
///
public bool RenderedField(string fieldName)
{
if (fieldName == null)
{
throw new ArgumentNullException(nameof(fieldName));
}
bool result;
RenderedFields.TryGetValue(fieldName, out result);
return result;
}
///
/// Updates to indicate has been rendered in this
/// <form>.
///
/// The full HTML name of a field that may have been rendered.
/// If true, the given has been rendered.
public void RenderedField(string fieldName, bool value)
{
if (fieldName == null)
{
throw new ArgumentNullException(nameof(fieldName));
}
RenderedFields[fieldName] = value;
}
}
}