This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
ViewContext.cs
234 lines (200 loc) · 8.16 KB
/
ViewContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// 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.IO;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
namespace Microsoft.AspNetCore.Mvc.Rendering
{
/// <summary>
/// Context for view execution.
/// </summary>
public class ViewContext : ActionContext
{
private FormContext _formContext;
private DynamicViewData _viewBag;
/// <summary>
/// Creates an empty <see cref="ViewContext"/>.
/// </summary>
/// <remarks>
/// The default constructor is provided for unit test purposes only.
/// </remarks>
public ViewContext()
{
ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), ModelState);
}
/// <summary>
/// Initializes a new instance of <see cref="ViewContext"/>.
/// </summary>
/// <param name="actionContext">The <see cref="ActionContext"/>.</param>
/// <param name="view">The <see cref="IView"/> being rendered.</param>
/// <param name="viewData">The <see cref="ViewDataDictionary"/>.</param>
/// <param name="tempData">The <see cref="ITempDataDictionary"/>.</param>
/// <param name="writer">The <see cref="TextWriter"/> to render output to.</param>
/// <param name="htmlHelperOptions">The <see cref="HtmlHelperOptions"/> to apply to this instance.</param>
public ViewContext(
ActionContext actionContext,
IView view,
ViewDataDictionary viewData,
ITempDataDictionary tempData,
TextWriter writer,
HtmlHelperOptions htmlHelperOptions)
: base(actionContext)
{
if (actionContext == null)
{
throw new ArgumentNullException(nameof(actionContext));
}
if (view == null)
{
throw new ArgumentNullException(nameof(view));
}
if (viewData == null)
{
throw new ArgumentNullException(nameof(viewData));
}
if (tempData == null)
{
throw new ArgumentNullException(nameof(tempData));
}
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}
if (htmlHelperOptions == null)
{
throw new ArgumentNullException(nameof(htmlHelperOptions));
}
View = view;
ViewData = viewData;
TempData = tempData;
Writer = writer;
FormContext = new FormContext();
ClientValidationEnabled = htmlHelperOptions.ClientValidationEnabled;
Html5DateRenderingMode = htmlHelperOptions.Html5DateRenderingMode;
ValidationSummaryMessageElement = htmlHelperOptions.ValidationSummaryMessageElement;
ValidationMessageElement = htmlHelperOptions.ValidationMessageElement;
}
/// <summary>
/// Initializes a new instance of <see cref="ViewContext"/>.
/// </summary>
/// <param name="viewContext">The <see cref="ViewContext"/> to copy values from.</param>
/// <param name="view">The <see cref="IView"/> being rendered.</param>
/// <param name="viewData">The <see cref="ViewDataDictionary"/>.</param>
/// <param name="writer">The <see cref="TextWriter"/> to render output to.</param>
public ViewContext(
ViewContext viewContext,
IView view,
ViewDataDictionary viewData,
TextWriter writer)
: base(viewContext)
{
if (viewContext == null)
{
throw new ArgumentNullException(nameof(viewContext));
}
if (view == null)
{
throw new ArgumentNullException(nameof(view));
}
if (viewData == null)
{
throw new ArgumentNullException(nameof(viewData));
}
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}
FormContext = viewContext.FormContext;
ClientValidationEnabled = viewContext.ClientValidationEnabled;
Html5DateRenderingMode = viewContext.Html5DateRenderingMode;
ValidationSummaryMessageElement = viewContext.ValidationSummaryMessageElement;
ValidationMessageElement = viewContext.ValidationMessageElement;
ExecutingFilePath = viewContext.ExecutingFilePath;
View = view;
ViewData = viewData;
TempData = viewContext.TempData;
Writer = writer;
}
/// <summary>
/// Gets or sets the <see cref="FormContext"/> for the form element being rendered.
/// A default context is returned if no form is currently being rendered.
/// </summary>
public virtual FormContext FormContext
{
get => _formContext;
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
_formContext = value;
}
}
/// <summary>
/// Gets or sets a value that indicates whether client-side validation is enabled.
/// </summary>
public bool ClientValidationEnabled { get; set; }
/// <summary>
/// Set this property to <see cref="Html5DateRenderingMode.CurrentCulture" /> to have templated helpers such as
/// <see cref="IHtmlHelper.Editor" /> and <see cref="IHtmlHelper{TModel}.EditorFor" /> render date and time
/// values using the current culture. By default, these helpers render dates and times as RFC 3339 compliant strings.
/// </summary>
public Html5DateRenderingMode Html5DateRenderingMode { get; set; }
/// <summary>
/// Element name used to wrap a top-level message generated by <see cref="IHtmlHelper.ValidationSummary"/> and
/// other overloads.
/// </summary>
public string ValidationSummaryMessageElement { get; set; }
/// <summary>
/// Element name used to wrap a top-level message generated by <see cref="IHtmlHelper.ValidationMessage"/> and
/// other overloads.
/// </summary>
public string ValidationMessageElement { get; set; }
/// <summary>
/// Gets the dynamic view bag.
/// </summary>
public dynamic ViewBag
{
get
{
if (_viewBag == null)
{
_viewBag = new DynamicViewData(() => ViewData);
}
return _viewBag;
}
}
/// <summary>
/// Gets or sets the <see cref="IView"/> currently being rendered, if any.
/// </summary>
public IView View { get; set; }
/// <summary>
/// Gets or sets the <see cref="ViewDataDictionary"/>.
/// </summary>
public ViewDataDictionary ViewData { get; set; }
/// <summary>
/// Gets or sets the <see cref="ITempDataDictionary"/> instance.
/// </summary>
public ITempDataDictionary TempData { get; set; }
/// <summary>
/// Gets or sets the <see cref="TextWriter"/> used to write the output.
/// </summary>
public TextWriter Writer { get; set; }
/// <summary>
/// Gets or sets the path of the view file currently being rendered.
/// </summary>
/// <remarks>
/// The rendering of a view may involve one or more files (e.g. _ViewStart, Layouts etc).
/// This property contains the path of the file currently being rendered.
/// </remarks>
public string ExecutingFilePath { get; set; }
public FormContext GetFormContextForClientValidation()
{
return ClientValidationEnabled ? FormContext : null;
}
}
}