thoughts on coding

January 17, 2009

Extensions to the ASP.NET MVC Contribution project

Filed under: Uncategorized — Frantisek @ 9:04 am

I work on a web project where I use ASP.NET MVC.  I hate the spaghetti code and I’m quite happy that MVCContrib added a new usefull extensions to the core MVC generic like Grid and generic ActionLink.

While using them I found I miss some features which I decided to add there. I explain both extensions  on the complex example.

          new Hash(empty => "There are no data", Class=>"list w99p"),
          columns =>
              columns.For("Children").Do(item =>
                  "<td>{0}</td>".FormatWith(Html.ActionLink<CarController>(c => c.Edit(item.ID), "...", new { Class = "none left" })
                columns.For(item => item.Code, "Code")
                    .Attrs((item, index) => new {style = "color:" + (index%2 == 0 ? "red" : "blue") + ";"})
                    .HeaderAttrs(new {style="color:red;"});
                  item => Html.ActionLink<CarController>(c => c.Edit(item.ID), item.Name, new { Class="none left" }), "Name").DoNotEncode();
                  item => Html.ActionLink<CarController>(
                      c => c.Delete(item.ID), "<span class=\"redC\">X</span>",
                      new { Class = "none left", onclick = "Assure(event, 'DoDelete');", title = "Delete" },
                  "Delete").DoNotEncode().Attrs((d,i) => new { Class = "w30 redC" });
            , sections => sections.RowAttrs((item, index) => new { Class = "Prenatal {0}".FormatWith(index % 2 == 0 ? "odd" : "") }));

GRID  extensions

Formatting the cell, the cell header, the row.

You can find many wishes from the users on the discussion forum of MVC grid regarding formatting the cells, headers, rows. It’s possible with current MVCContrib but using a spaghetti code which I dont like. So I decided to exted MVCContrib  with this missing simple functionality.

              columns.For(item => item.Code, "Code")
                .Attrs((item, index) => new { style = (item.Sale > 0 ? "color:red" : "") + ";" })
                .HeaderAttrs(new { style = "color:red;" });

Using method Attrsit’s possible to specify the cell’s HTML attributes which are calculated per each item! in the model (i.e. if  it’s necessary to change the row background based on the data in the item). In the example I use second argument (index of the item in the list) to set color of the cell.

Using method HeaderAttrs it’s possible to set the HTML attributes of the cell header. In this case there is no reason to calculate the HTML attributes per item as the header is rendered as the first row in the grid. The user can specify the attribute as an annonymous object.

The similla approach is used for the rows, too.

sections => sections.RowAttrs((item, index) => new { Class = "Prenatal {0}".FormatWith(index % 2 == 0 ? "odd" : "") })

Using RowAttrs it is possible to set the HTML attributes of the row (TR tag) which are calculated per item! as mentioned above.

Action Link extensions

There are many posts, hack, complaint about using ActionLink which should have as a content an Image or  any HTML. The default implementation of ActionLink encodes the linkText by default. Using my extention it is possible to specify a LinkOption if I need to encode or not  the link content.

                item => Html.ActionLink<CarController>(
                          c => c.Delete(item.ID), // href
                          "<span class=\"redC\">X</span>", // LINK CONTENT
                          new { Class = "none left", onclick = "Assure(event, 'DoDelete');", title = "Delete" }, // html attributes
                          LinkOptions.PlainContent); // LINK OPTIONS

That’s all for now.  I hope this post can be helpfull for you.

The source code and binaries can be found at here



  1. Hey, I liked very much your extensions, but the link for the code and binaries is broken. Could you please e-mail me your extensions?
    I’d like to use them on my projects.


    Teoni Valois

    Comment by Teoni Valois — January 21, 2009 @ 2:14 pm

    • Thanks for the info that the link is broken. I’ve sent you the file. The link is corrected. The file contains whole MVCContrib with my extensions.

      Comment by Frantisek — January 21, 2009 @ 9:58 pm

  2. Hi Frantisek

    I’ve now incorporated most of these features into the rewritten version of the mvccontrib grid. I changed the attribute methods to take an IDictionary rather than an anonymous type. If you still want to use an anonymous type then it should be trivial to add this behaviour with extension methods.


    Comment by Jeremy Skinner — February 26, 2009 @ 7:47 pm

  3. Hi Jeremy, thanks for your commment and the changes you made in MVCContrib project. I analyzed it and write about it in my next post:

    Can you send me your opinion?


    Comment by Frantisek — March 5, 2009 @ 12:40 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at

%d bloggers like this: