|
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.
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