File: ModelMethodContext.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (80 lines) | stat: -rw-r--r-- 3,601 bytes parent folder | download | duplicates (7)
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
namespace System.Web.UI.WebControls {
    using System;
    using System.Web.ModelBinding;

    /// <summary>
    /// Used to invoke <see cref='System.Web.UI.Page.UpdateModel' />/<see cref='System.Web.UI.Page.TryUpdateModel' /> 
    /// methods where <see cref='System.Web.UI.Page' /> is directly not accessible.
    /// An example is a custom class defining the Select/Update/Delete/InsertMethod properties for 
    /// data binding can have a parameter of type <see cref='System.Web.UI.WebControls.ModelMethodContext' />
    /// in the above methods and use it to invoke the above methods. Alternately instead of a method parameter,
    /// <see cref='System.Web.UI.WebControls.ModelMethodContext.Current' /> can also be used within the 
    /// Select/Update/Delete/InsertMethod code to invoke TryUpdateModel/UpdateModel methods.
    /// </summary>
    public class ModelMethodContext {
        private Page _page;

        public ModelMethodContext(Page page) {
            if (page == null) {
                throw new ArgumentNullException("page");
            }

            _page = page;
        }

        public ModelStateDictionary ModelState {
            get {
                return _page.ModelState;
            }
        }

        /// <summary>
        /// Gets the ModelMethodContext corresponding to the <see cref='System.Web.UI.Page' /> from <see cref='System.Web.HttpContext.Current' />.
        /// Can be null when the current request is not for a <see cref='System.Web.UI.Page' />.
        /// </summary>
        public static ModelMethodContext Current {
            get {
                Page page = HttpContext.Current.Handler as Page;
                if (page != null) {
                    return new ModelMethodContext(page);
                }

                return null;
            }
        }

        /// <summary>
        /// Updates the model object from the values within a databound control. This must be invoked 
        /// within the Select/Update/Delete/InsertMethods used for data binding.
        /// Throws an exception if the update fails.
        /// </summary>
        public virtual void UpdateModel<TModel>(TModel model) where TModel : class {
            _page.UpdateModel<TModel>(model);
        }

        /// <summary>
        /// Updates the model object from the values provided by given valueProvider.
        /// Throws an exception if the update fails.
        /// </summary>
        public virtual void UpdateModel<TModel>(TModel model, IValueProvider valueProvider) where TModel : class {
            _page.UpdateModel<TModel>(model, valueProvider);
        }

        /// <summary>
        /// Attempts to update the model object from the values provided by given valueProvider.
        /// </summary>
        /// <returns>True if the model object is updated succesfully with valid values. False otherwise.</returns>
        public virtual bool TryUpdateModel<TModel>(TModel model) where TModel : class {
            return _page.TryUpdateModel<TModel>(model);
        }

        /// <summary>
        /// Attempts to update the model object from the values within a databound control. This
        /// must be invoked within the Select/Update/Delete/InsertMethods used for data binding. 
        /// </summary>
        /// <returns>True if the model object is updated succesfully with valid values. False otherwise.</returns>
        public virtual bool TryUpdateModel<TModel>(TModel model, IValueProvider valueProvider) where TModel : class {
            return _page.TryUpdateModel<TModel>(model, valueProvider);
        }
    }
}