Click here to Skip to main content
15,895,799 members
Articles / Web Development / ASP.NET

Web Page Extensions to Associate Domain Models With Web Forms

Rate me:
Please Sign up or sign in to vote.
4.53/5 (4 votes)
29 Jan 2011CPOL3 min read 30.7K   13  
An extension type associates an ASPX view page with a domain model object.
namespace System.Web.UI
{
    using System.Reflection;

    /// <summary>
    /// Base class for all srongly typed .aspx view pages.
    /// </summary>
    /// <typeparam name="T">Type of the domain model object.</typeparam>
    public abstract class ViewPage<T> : Page
    {
        ModelStateValidator<T> modelState;

        /// <summary>
        /// Get or set domain model type associated with this view page.
        /// </summary>
        protected virtual T Model
        {
            get;
            set;
        }

        /// <summary>
        /// Get html helper object that provides support for rendering html elements.
        /// </summary>
        protected virtual HtmlHelper<T> Html
        {
            get
            {
                return new HtmlHelper<T>(Model, ModelState);
            }
        }

        /// <summary>
        /// Get model state validator dictionary object that contains model-validation errors.
        /// </summary>
        protected virtual ModelStateValidator<T> ModelState
        {
            get
            {
                if (modelState == null)
                {
                    modelState = new ModelStateValidator<T>(Model);
                }
                return modelState;
            }
        }

        /// <summary>
        /// Raises the System.Web.UI.Control.Init event to initialize the page.
        /// </summary>
        /// <param name="e">System.EventArgs containing event data.</param>
        protected override void OnInit(EventArgs e)
        {
            T model = Activator.CreateInstance<T>();            
            if (this.IsPostBack)
            {
                string modelName = typeof(T).Name;
                HttpRequest request = HttpContext.Current.Request;               
                foreach (PropertyInfo property in typeof(T).GetProperties())
                {
                    if (request[modelName + "_" + property.Name] != null)
                    {
                        property.SetValue(model, Convert.ChangeType(request[modelName + "_" + property.Name], property.PropertyType), null);
                    }
                }                
            }
            Model = model;
            base.OnInit(e);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Cognizant Technology Solutions
India India
I work as a Technology Specialist in Cognizant Technology Solutions. I have 1.5 decades of experience in Software Development and focuses on Microsoft Web Technologies, JavaScript Frameworks, Azure Services and DevOps

Comments and Discussions