Updates from August, 2011 Toggle Comment Threads | Keyboard Shortcuts

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

    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())
        {
            @Html.ValidationSummary(true)
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Name)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.CollectionName)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.CollectionName)
                @Html.ValidationMessageFor(model => model.CollectionName)
            </div>
    
            <div class="form-buttons">
                <input type="submit" value="@this.GlobalResource(Resources.Language.Create)" />
            </div>
        }
    

    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.ValidationSummary(true)
            @Html.EditorBlockFor(model => model.Name);
            @Html.EditorBlockFor(model => model.CollectionName);
    
            <div class="form-buttons">
                <input type="submit" value="@this.GlobalResource(Resources.Language.Create)" />
            </div>
        }
    

    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 10:06 pm on August 10, 2011 Permalink | Reply
    Tags: assemblyinfo, sharedassemblyinfo   

    How to put shared .NET solution assembly information into one single file 

    When working with .NET solutions that contains several projects, I have found it to be a real hassle to manage version numbers and other shared information for the various assemblies. Each time I want to change the version, I have had to open up the AssemblyInfo.cs file of each project and edit the changed information.

    Well, from now on, it’s going to be a walk in the park, since I finally sat down for ten minutes and figured out how to share assembly information between projects. Ten minutes, that’s all it took to find the piece of info I needed and apply it to my .NET Extensions library.

    So, how to do it? The answer is here, thanks Jeremy!

    In short, the secret is to create a shared assembly information file (e.g. SharedAssemblyInfo.cs in the solution root folder or another solution folder) and link it into each project. To keep things concise, move it into the Properties folder of each project after linking it into the project. Once the shared information file is in place, make sure to delete any duplicate information from your original AssemblyInfo.cs file.

    That is all there is to it. Quite simple, right?

     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel