Click here to Skip to main content
15,881,882 members
Articles / Web Development / XHTML

Custom controls in ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
4.76/5 (31 votes)
8 Jan 2009CPOL8 min read 158.8K   2.9K   95  
Control library for rendering custom HTML in ASP.NET MVC applications.
using System;
using System.IO;
using System.Text;
using System.Web.Mvc;

namespace MvcControls
{
	public class MvcValidationSummary : MvcControl
	{
		#region MvcControlBuilder Members

		protected override void Initialise(ViewContext viewContext)
		{
			if (viewContext == null)
			{
				throw new ArgumentNullException("viewContext");
			}

			ViewDataDictionary viewData = viewContext.ViewData;

			if (viewData == null)
			{
				throw new ArgumentNullException("viewData");
			}

			if (viewData.ModelState.IsValid)
			{
				return;
			}

			StringBuilder listItems = new StringBuilder();

			foreach (var modelStateKvp in viewData.ModelState)
			{
				foreach (var modelError in modelStateKvp.Value.Errors)
				{
					TagBuilder listItem = new TagBuilder("li");
					listItem.SetInnerText(modelError.ErrorMessage);

					listItems.AppendLine(listItem.ToString());
				}
			}

			InnerHtml = listItems.ToString();
		}

		#endregion

		public MvcValidationSummary()
			: base("ul")
		{
			// Set a default CSS class.
			Class = "summary-validation-errors";
		}
	}
}

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
Web Developer
United Kingdom United Kingdom
Software engineer.

Blog: http://andrewgunn.blogspot.com/
Twitter: andrewgunn

Comments and Discussions