namespace System.Web.UI.WebControls { using System; using System.Web.ModelBinding; /// /// Used to invoke / /// methods where 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 /// in the above methods and use it to invoke the above methods. Alternately instead of a method parameter, /// can also be used within the /// Select/Update/Delete/InsertMethod code to invoke TryUpdateModel/UpdateModel methods. /// 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; } } /// /// Gets the ModelMethodContext corresponding to the from . /// Can be null when the current request is not for a . /// public static ModelMethodContext Current { get { Page page = HttpContext.Current.Handler as Page; if (page != null) { return new ModelMethodContext(page); } return null; } } /// /// 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. /// public virtual void UpdateModel(TModel model) where TModel : class { _page.UpdateModel(model); } /// /// Updates the model object from the values provided by given valueProvider. /// Throws an exception if the update fails. /// public virtual void UpdateModel(TModel model, IValueProvider valueProvider) where TModel : class { _page.UpdateModel(model, valueProvider); } /// /// Attempts to update the model object from the values provided by given valueProvider. /// /// True if the model object is updated succesfully with valid values. False otherwise. public virtual bool TryUpdateModel(TModel model) where TModel : class { return _page.TryUpdateModel(model); } /// /// 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. /// /// True if the model object is updated succesfully with valid values. False otherwise. public virtual bool TryUpdateModel(TModel model, IValueProvider valueProvider) where TModel : class { return _page.TryUpdateModel(model, valueProvider); } } }