New test.
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / Html / MvcForm.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\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
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc.Html {\r
14     using System;\r
15     using System.IO;\r
16 \r
17     public class MvcForm : IDisposable {\r
18 \r
19         private bool _disposed;\r
20         private readonly FormContext _originalFormContext;\r
21         private readonly ViewContext _viewContext;\r
22         private readonly TextWriter _writer;\r
23 \r
24         [Obsolete("The recommended alternative is the constructor MvcForm(ViewContext viewContext).", true /* error */)]\r
25         public MvcForm(HttpResponseBase httpResponse) {\r
26             if (httpResponse == null) {\r
27                 throw new ArgumentNullException("httpResponse");\r
28             }\r
29 \r
30             _writer = httpResponse.Output;\r
31         }\r
32 \r
33         public MvcForm(ViewContext viewContext) {\r
34             if (viewContext == null) {\r
35                 throw new ArgumentNullException("viewContext");\r
36             }\r
37 \r
38             _viewContext = viewContext;\r
39             _writer = viewContext.Writer;\r
40 \r
41             // push the new FormContext\r
42             _originalFormContext = viewContext.FormContext;\r
43             viewContext.FormContext = new FormContext();\r
44         }\r
45 \r
46         public void Dispose() {\r
47             Dispose(true /* disposing */);\r
48             GC.SuppressFinalize(this);\r
49         }\r
50 \r
51         protected virtual void Dispose(bool disposing) {\r
52             if (!_disposed) {\r
53                 _disposed = true;\r
54                 _writer.Write("</form>");\r
55 \r
56                 // output client validation and restore the original form context\r
57                 if (_viewContext != null) {\r
58                     _viewContext.OutputClientValidation();\r
59                     _viewContext.FormContext = _originalFormContext;\r
60                 }\r
61             }\r
62         }\r
63 \r
64         public void EndForm() {\r
65             Dispose(true);\r
66         }\r
67 \r
68     }\r
69 }\r