Tagged: validationmessagefor Toggle Comment Threads | Keyboard Shortcuts

  • danielsaidi 4:49 pm on August 28, 2011 Permalink | Reply
    Tags: , editorblockfor, editorfor, , html helper, labelfor, validationmessagefor   

    EditorBlockFor HTML helper 

    In ASP.NET MVC, Microsoft has done a great job with the various HTML helpers that can be used in a form context, such as LabelFor, EditorFor, ValidationMessageFor etc.

    However, despite these helpers, the HTML markup still tend to become rather tedious and repetitive. For instance, this HTML generates a form that can be used t0 create groups in a web application that I am currently working on:

        @using (Html.BeginForm())
            <div class="editor-label">
                @Html.LabelFor(model => model.Name)
            <div class="editor-field">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            <div class="editor-label">
                @Html.LabelFor(model => model.CollectionName)
            <div class="editor-field">
                @Html.EditorFor(model => model.CollectionName)
                @Html.ValidationMessageFor(model => model.CollectionName)
            <div class="form-buttons">
                <input type="submit" value="@this.GlobalResource(Resources.Language.Create)" />

    That is quite a lot of code for handling two single properties…and the two editor blocks look rather similar, don’t you think?

    I therefore decided to write a small HTML helper extension method – EditorBlockFor – that can be used to generate an editor block (label, editor and validation message).

    Using this new helper, the resulting form becomes a lot shorter and a lot easier to handle:

        @using (Html.BeginForm())
            @Html.EditorBlockFor(model => model.Name);
            @Html.EditorBlockFor(model => model.CollectionName);
            <div class="form-buttons">
                <input type="submit" value="@this.GlobalResource(Resources.Language.Create)" />

    As you see, the method is only to be used if you want to follow the conventions that are used for auto-generated ASP.NET MVC form code. But if you do…you can save a lot of keystrokes.

    I am not that familiar with the MvcHtmlString type, which the native methods return, so returning an IHtmlString instead of MvcHtmlString could be a big no no that I do not know about.

    Please let me know if I have ruined the order of the universe.

  • danielsaidi 12:58 pm on January 13, 2011 Permalink | Reply
    Tags: , , razor, , validationmessagefor, validationsummary   

    Adding Spark to ASP.NET MVC 2.0 

    After almost a year’s curiosity, with other stuff stealing my time, I have finally some time to have a look at the Spark View Engine. Since the Razor View Engine will be shipped together with ASP.NET MVC 3.0, I decided to give Spark a shot before Razor.

    I will not go into detail about what Spark is, what it does etc. etc. The official web site has some good info and this blog helped me get started. I will just describe how to get Spark up and running and get it to play alongside a default ASP.NET MVC application.

    Get Spark up and running

    The first thing we have to do is to add Spark to our application. Visit the Spark web site, download and unzip the latest release, add it to your solution and add references to Spark.dll and Spark.Web.Mvc.dll.

    Of course, if you use NuGet, just right-click the references folder and choose Add package reference…

    …then search for spark and install sparkmvc.

    This will automatically add most of what you need. Really convenient!

    However, for Spark to work, we need to register it when the application starts. Do this by adding the following to Application_Start in global.asax.cs:

       ViewEngines.Engines.Add(new SparkViewFactory());

    Finally, I recommend adding the following to web.config, although it is optional:

          <section name="spark" type="Spark.Configuration.SparkSectionHandler, Spark"/>
          <compilation debug="true" />
          <pages automaticEncoding="true" />

    That’s it – Spark is now added to your app and runs alongside the default Web Forms view engine. Any views not using Spark will still work.

    Converting views to Spark

    In short, all you have to do to convert your views (master pages, user controls and pages) to spark is to:

    • Rename Site.master to Application.spark
    • Rename all user controls to _NAME.spark (the underline makes it possible to add user controls as HTML elements)
    • Rename all pages to NAME.spark
    • In all master page, user control and page files, remove the topmost tag
    • In Application.spark, replace all ContentPlaceHolders tags with use spark tags
    • In all pages, replace all Content tags with contentspark tags

    You also have to replace:

       <%   =>   ${

    For strongly types views,you can type ViewData and the Model like this:

       <viewdata Message="string" model="SparkTemplate.Models.LogOnModel" />

    Note that the name of the ViewData key is defined as is while the model tag is defined in lower-case. You can then access the model like this:


    Other than that, everything just seem to work.

    Worth noticing is that you should use ! instead of $ for ValidationSummary and ValidationMessageFor Рotherwise, empty strings will render the full Spark expression.

Compose new post
Next post/Next comment
Previous post/Previous comment
Show/Hide comments
Go to top
Go to login
Show/Hide help
shift + esc