// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.IO; using Microsoft.AspNet.Mvc.Rendering; namespace Microsoft.AspNet.Mvc { public class ViewContext : ActionContext { // We need a default FormContext if the user uses html
instead of an MvcForm private readonly FormContext _defaultFormContext = new FormContext(); private FormContext _formContext; private DynamicViewData _viewBag; public ViewContext( [NotNull] ActionContext actionContext, [NotNull] IView view, [NotNull] ViewDataDictionary viewData, [NotNull] TextWriter writer) : base(actionContext) { View = view; ViewData = viewData; Writer = writer; _formContext = _defaultFormContext; ClientValidationEnabled = true; ValidationSummaryMessageElement = "span"; ValidationMessageElement = "span"; } public ViewContext( [NotNull] ViewContext viewContext, [NotNull] IView view, [NotNull] ViewDataDictionary viewData, [NotNull] TextWriter writer) : base(viewContext) { _formContext = viewContext.FormContext; ClientValidationEnabled = viewContext.ClientValidationEnabled; Html5DateRenderingMode = viewContext.Html5DateRenderingMode; ValidationSummaryMessageElement = viewContext.ValidationSummaryMessageElement; ValidationMessageElement = viewContext.ValidationMessageElement; View = view; ViewData = viewData; Writer = writer; } public virtual FormContext FormContext { get { return _formContext; } set { // Never return a null form context, this is important for validation purposes. _formContext = value ?? _defaultFormContext; } } public bool ClientValidationEnabled { get; set; } /// /// Set this property to to have templated helpers such as /// and render date and time /// values as RFC 3339 compliant strings. By default these helpers render dates and times using the current /// culture. /// public Html5DateRenderingMode Html5DateRenderingMode { get; set; } /// /// Element name used to wrap a top-level message generated by and /// other overloads. /// public string ValidationSummaryMessageElement { get; set; } /// /// Element name used to wrap a top-level message generated by and /// other overloads. /// public string ValidationMessageElement { get; set; } public dynamic ViewBag { get { if (_viewBag == null) { _viewBag = new DynamicViewData(() => ViewData); } return _viewBag; } } public IView View { get; set; } public ViewDataDictionary ViewData { get; set; } public TextWriter Writer { get; set; } public FormContext GetFormContextForClientValidation() { return (ClientValidationEnabled) ? FormContext : null; } } }