-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathModelMethodContext.cs
More file actions
80 lines (70 loc) · 3.52 KB
/
ModelMethodContext.cs
File metadata and controls
80 lines (70 loc) · 3.52 KB
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 successfully 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 successfully with valid values. False otherwise.</returns>
public virtual bool TryUpdateModel<TModel>(TModel model, IValueProvider valueProvider) where TModel : class {
return _page.TryUpdateModel<TModel>(model, valueProvider);
}
}
}