Html Conventions in ASP.net MVC

Just today, Jimmy wrote a great post on using HTML conventions in ASP.net MVC using FubuMvc’s core HTML conventions. Go ahead a read it. I’ll wait here.

The problem with this is that it heavily relies on StructureMap (FWIW I love SM, but its overkill here), and doesn’t quite fit into the way ASP.net MVC does things. It also requires a lot of setup. I found this out almost 2 years ago now, and so SchoStack.Web was born. Over the coming posts I will go through what’s included in the library, but here is how you quickly get up and running with the default conventions (these represent the defaults that render lists, dropdowns, checkbox’s which are all ASP.net MVC compliant).

  1. Create new mvc project.
  2. install-package schostack.web
  3. Replace System.Web.Html namespaces in Views/web.config with the following
    <add namespace="SchoStack.Web.Html" />
    <add namespace="SchoStack.Web.Html.Form" />
  4. In Application_Start add:
    HtmlConventionFactory.Add(new DefaultHtmlConventions());
    HtmlConventionFactory.Add(new DataAnnotationHtmlConventions());
    HtmlConventionFactory.Add(new DataAnnotationValidationHtmlConventions());
  5. Thats it.

The following are the three main helpers used to generate all HTML based on your conventions.

@Html.Input(x=>x.Name)
@Html.Display(x=>x.Name)
@Html.Label(x=>x.Name)

All the usual MVC helpers are available as well like ValidationSummary and ValidationMessage.

I’ll try to cover the following things in upcoming posts, but if you have any questions or issues you can ask/raise them on github.

1. Creating your own conventions
2. Closer look at out of the box conventions
3. Validation (Server and Client validation using either FluentValidation and Data Annotations driven off the input model, not the viewmodel)
4. Foreach Loop Helpers
5. Url Generation and Routing

Adam

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>