blog.mha.dk
The on-line blog of Michael Holm Andersen

Looking at ASP.NET MVC 3

Monday, 17 January 2011 09:51 by mha

I’m in the process of re-writing www.aspnethotel.dk to use ASP.NET MVC 3 and are planning to use the new view engine (“Razor”). For more info about this, take a look at http://www.asp.net/mvc/mvc3

Categories:   ASP.NET MVC
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

ASP.NET MVC Best Practices

Wednesday, 10 February 2010 07:49 by mha

Kazi Manzur Rashid has put together a blog post (in 2 parts) about ASP.NET MVC Best Practices which is worth reading if you’re working with ASP.NET MVC.

ASP.NET MVC Best Practices (Part 1)
ASP.NET MVC Best Practices (Part 2)

Categories:   ASP.NET MVC
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

ASP.NET MVC and ASCX

Tuesday, 9 February 2010 14:51 by mha

Jeffrey Palermo has an inspiring article about templating and (why not) to use .ascx aka UserControls in ASP.NET MVC – I’ll definitely be using this approach:

http://jeffreypalermo.com/blog/asp-net-mvc-and-the-templated-partial-view-death-to-ascx/

jQuery IntelliSense on ASP.NET MVC

Tuesday, 9 February 2010 12:51 by mha

If you’re using ASP.NET MVC, you’re probably also using jQuery and hence would like to have IntelliSense support in your ViewPages. According to Scott Gu all you need to do is to make sure you’re having the “-vsdoc.js” file in your script folder (e.g. “jquery-1.3.2-vsdoc.js”) and VS2008 (with the patch installed!) will find this file and jQuery IntelliSense works – however as it turns out if you’re using a path which starts with “/” (as you should!) VS2008 is unable locate the “-vsdoc.js” file and you will not have any jQuery IntelliSense support.

So there’s a little more to know about how exactly jQuery IntelliSense actually works in order to make this work correctly (not to mention IntelliSense support in .ascx files). Luckily Jouni Heikniemi have written a great article about this – check it out:

http://www.heikniemi.net/hardcoded/2009/08/jquery-intellisense-on-asp-net-mvc/

LINQ Projection and SelectListItem

Sunday, 17 January 2010 15:16 by mha

A small example of where LINQ Projection might come in handy is when you’re working with MVC. In the below example LINQ Projection is used to transform a enumeration of EmployeeCategory instances into an enumeration of SelectListItem instances:

var rep = new mhaRepository();
var person = rep.GetPersonByID(id);
ViewData["EmployeeCategories"] = from c in rep.EmployeeCategories
                         select new SelectListItem
                         {
                             Text = c.Name,
                             Value = c.ID.ToString(),
                             Selected = (c.ID == person.CategoryID)
                         };

In the View all that is needed to render the DropDownList is this:

<%= Html.DropDownList("EmployeeCategories") %>

Categories:   LINQ | ASP.NET MVC
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

ViewData and the null-coalescing operator

Wednesday, 30 December 2009 08:12 by mha

If you’re using ASP.NET MVC and are using the ViewData collection you might want to familiar yourself with the ?? operator.

The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand.

Knowing this we can use the below syntax to e.g. display the title of a page in the browser with a value from ViewData and if this is null instead display a default value.

<%= ViewData["title"] ?? "Home Page" %>

MSDN has more info about the ?? operator.

Categories:   C# | ASP.NET MVC
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed