1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\r
5 * This software is subject to the Microsoft Public License (Ms-PL).
\r
6 * A copy of the license can be found in the license.htm file included
\r
7 * in this distribution.
\r
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\r
15 using System.Collections;
\r
16 using System.Diagnostics.CodeAnalysis;
\r
17 using System.Globalization;
\r
19 using System.Web.Script.Serialization;
\r
21 public class ViewContext : ControllerContext {
\r
23 private const string _clientValidationScript = @"<script type=""text/javascript"">
\r
25 if (!window.mvcClientValidationMetadata) {{ window.mvcClientValidationMetadata = []; }}
\r
26 window.mvcClientValidationMetadata.push({0});
\r
30 // Some values have to be stored in HttpContext.Items in order to be propagated between calls
\r
31 // to RenderPartial(), RenderAction(), etc.
\r
32 private static readonly object _clientValidationEnabledKey = new object();
\r
33 private static readonly object _formContextKey = new object();
\r
34 private static readonly object _lastFormNumKey = new object();
\r
36 private Func<string> _formIdGenerator;
\r
38 // parameterless constructor used for mocking
\r
39 public ViewContext() {
\r
42 [SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors",
\r
43 Justification = "The virtual property setters are only to support mocking frameworks, in which case this constructor shouldn't be called anyway.")]
\r
44 public ViewContext(ControllerContext controllerContext, IView view, ViewDataDictionary viewData, TempDataDictionary tempData, TextWriter writer)
\r
45 : base(controllerContext) {
\r
46 if (controllerContext == null) {
\r
47 throw new ArgumentNullException("controllerContext");
\r
50 throw new ArgumentNullException("view");
\r
52 if (viewData == null) {
\r
53 throw new ArgumentNullException("viewData");
\r
55 if (tempData == null) {
\r
56 throw new ArgumentNullException("tempData");
\r
58 if (writer == null) {
\r
59 throw new ArgumentNullException("writer");
\r
63 ViewData = viewData;
\r
65 TempData = tempData;
\r
68 public virtual bool ClientValidationEnabled {
\r
70 return (HttpContext.Items[_clientValidationEnabledKey] as bool?).GetValueOrDefault();
\r
73 HttpContext.Items[_clientValidationEnabledKey] = value;
\r
77 public virtual FormContext FormContext {
\r
79 return HttpContext.Items[_formContextKey] as FormContext;
\r
82 HttpContext.Items[_formContextKey] = value;
\r
86 internal Func<string> FormIdGenerator {
\r
88 if (_formIdGenerator == null) {
\r
89 _formIdGenerator = DefaultFormIdGenerator;
\r
91 return _formIdGenerator;
\r
94 _formIdGenerator = value;
\r
98 [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly",
\r
99 Justification = "The property setter is only here to support mocking this type and should not be called at runtime.")]
\r
100 public virtual TempDataDictionary TempData {
\r
105 public virtual IView View {
\r
110 [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly",
\r
111 Justification = "The property setter is only here to support mocking this type and should not be called at runtime.")]
\r
112 public virtual ViewDataDictionary ViewData {
\r
117 public virtual TextWriter Writer {
\r
122 private string DefaultFormIdGenerator() {
\r
123 int formNum = IncrementFormCount(HttpContext.Items);
\r
124 return String.Format(CultureInfo.InvariantCulture, "form{0}", formNum);
\r
127 internal FormContext GetFormContextForClientValidation() {
\r
128 return (ClientValidationEnabled) ? FormContext : null;
\r
131 private static int IncrementFormCount(IDictionary items) {
\r
132 object lastFormNum = items[_lastFormNumKey];
\r
133 int newFormNum = (lastFormNum != null) ? ((int)lastFormNum) + 1 : 0;
\r
134 items[_lastFormNumKey] = newFormNum;
\r
138 public void OutputClientValidation() {
\r
139 FormContext formContext = GetFormContextForClientValidation();
\r
140 if (formContext == null) {
\r
141 return; // do nothing
\r
144 string scriptWithCorrectNewLines = _clientValidationScript.Replace("\r\n", Environment.NewLine);
\r
145 string validationJson = formContext.GetJsonValidationMetadata();
\r
146 string formatted = String.Format(CultureInfo.InvariantCulture, scriptWithCorrectNewLines, validationJson);
\r
148 Writer.Write(formatted);
\r
149 FormContext = null;
\r