Blog

  • Simple and Custom Validation in MVC 3

    This is the first of a series on validation and business rules in MVC 3. I'll start out simple and we'll work out way up to the more complex. I'm going to build on the solution created in the previous blog post (Delete Like a Rock Star) so go there and get set up. Continue reading...

  • Delete Like a Rock Star with MVC3, Ajax and jQuery

    Sure you can be a chump and use the out-of-the-box delete functionality that the MvcScaffolding gives you. Or you can use a little jQuery and delete like a rock star. Get your project set up first. Create a new MVC3 web app in Visual Studio 2010. If you have SP1 you have a choice now: internet or intranet. I'll assume we're creating an internet app but it doesn't matter really. Next add a new model called Widget: Continue reading...

  • Render HTML for Menu in MVC 3 Using SiteMapProvider

    There are several good jQuery menu plugins but I like the old skool ASP.NET SiteMapProvider because I can declare my app roles in there and get security trimming for free. Add and configure the default XmlSiteMapProvider or customize one to override the IsAccessibleToUser method. Your choice. Then add this MenuHelper static class to your project:  public static class MenuHelper { public static MvcHtmlString Menu(this HtmlHelper helper) { var sb = new StringBuilder();  sb.Append("<ul id='nav'>");  // Render each top level node var topLevelNodes = SiteMap.RootNode.ChildNodes; foreach (SiteMapNode node in topLevelNodes) { sb.AppendLine("<li>");  sb.AppendFormat("<a href='{0}'>{1}</a>", node.Url, helper.Encode(node.Title)); if (node.HasChildNodes) { sb.AppendLine("<ul>"); foreach (SiteMapNode childNode in node.ChildNodes) { sb.AppendLine("<li>"); sb.AppendFormat("<a href='{0}'>{1}</a>", childNode.Url, helper.Encode(childNode.Title)); sb.AppendLine("</li>"); } sb.AppendLine("</ul>"); }  sb.AppendLine("</li>"); }  // Close unordered list tag sb.Append("</ul>");  return new MvcHtmlString(sb.ToString()); } } If this is the first helper for the project then add the namespace where your MenuHelper class lives to your Views Web.config file: <system.web.webPages.razor> <host factoryType="System.Web.Mvc. … Continue reading...

  • More Awesomeness: Post JSON to an Action Method

    Fortunately I came late to the party and didn't develop MVC 2 web apps. I understand that back in those ancient days (about 6 months ago) you had to walk uphill both ways to post JSON data back to an action method. Phil Haack has a great blog post on the topic. Basically, you had to write a custom filter, register a JsonValueProviderFactory in application startup, and decorate your action method with the JSON serializer attributes. What a pain. In MVC 3 it's super easy to use AJAX to post JSON back to an action method. Here are the steps. Continue reading...

  • Implement jQuery FullCalendar Plugin in MVC 3

    Let's implement Shaw's awesome FullCalendar jQuery plugin with an MVC 3 project. I'll assume you've downloaded the bits and have included the fullcalendar.min.js script in your project. Here I want to illustrate two things: (1) populating the calendar with events from the database; and (2) handling the calendar's dayClick event to create a new event. Continue reading...

  • TruthWidget Launch Part 2

    TruthWidget.com is fully ported over to MVC 3 from Silverlight 4. I ran through all of my tests and so it's up and running live. The awesome part was being able to reuse Entity Framework 4 and the repository wrappers. I took the RIA web services and put them into MVC services. I've seen examples where the controller calls EF directly but that makes me uncomfortable so I wanted that additional layer of abstraction. The bulk of my work was throwing away the XAML and recreating Html (Razor) web pages. What I liked about my experience is how I can create HtmlHelpers (little helper methods that emit small chunks of HTML), partial views, and stitch them together to create the whole view. This enabled me to follow the DRY principle a lot more than in XAML where I tended to have big bloated views -- especially if I did it in Blend. I'm really happy with my experience. I'll blog on specific tips and tricks when I have time. For now I'll just say that everything just works in MVC 3. It's a real joy to work with it. Continue reading...

  • Silverlight Problems: Yet Another Plug In

    So I labored mightily last December on TruthWidget.com. Wanting to be hip and happening I implemented it in Silverlight 4 and RIA Services. It worked out well. I was pumped. So post-launch I asked a few non-technical friends to take it out for a spin. And then the problems started. They told me it didn't work. I quickly discovered most didn't know that they needed to install the Silverlight runtime. I was counting on the blue box of joy to do the work for me but it wasn't explicit enough and many didn't even look at it very closely. So following Tim Heuer's lead I dove into a custom installation experience. But the detection script reports Chrome as an unsupported browser. I could hack the script of course. But the problem is much deeper: my friends didn't want to install yet another plug in. They didn't understand why they needed to do it, had questions about it, and finally hesitated to take the simple 30 seconds to do it. I realized that if this is how my friends reacted what would complete strangers do? In the end I decided I needed to port the app over to a web application. MVC 3 to be specific and it's going great. More on that later. My takeaway from this experience is if you cannot control the end user's browser then stick with tried and true web applications. If it's an intranet app where you know your end user's environment and they need a rich-client experience then consider Silverlight 4. Continue reading...

  • WCF Debugging Tip

    WCF Debugging from Silverlight client-side:WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);Also, turn on debugging in the WCF service Web.config. You'll get detailed inner exception information. In the <serviceBehaviors> region: <serviceDebug includeExceptionDetailInFaults="true" />   Continue reading...

  • Active Directory Federation Services v2

    ADFS is based on Web Services architecture (WS-* specifications) and conforms to industry standards and protocols. This release supports Azure and would be a great choice for enterprises using AD as a credential store on the local domain who want to migrate to the cloud. Applications hosted on local IIS7 servers as well as in the Azure cloud can take advantage of single sign on over the extranet for the duration of the user's browser session. See MSDN for developing federation-aware applications. Continue reading...

  • IIS 7 Errors

    On my sandbox (WinOS 2008) I installed Web Platform Installer 2.0. Then through the PI I installed Frameworks and Runtime (.NET Framework 4.0), IIS 7, and Web Deploy Tool 1.1. I took a vanilla ASP.NET app from the new VS2010 project template and deployed to IIS 7 using the new packaging tools. Depending upon whether the app pool was integrated pipeline or classic I got these errors: Continue reading...

  • Lambda Expressions with NHibernate.Linq

    In the last two posts below I talked about creating a templated DataService class and also mused about whether UI data queries really were a cross-cutting concern. I'm agnostic about the question really; however, I do have one practical concern. I don't want to expose methods that take any old HQL query that the client sends my way. I'd like to have a controlled way of extending database queries down to the UI. Continue reading...

  • UI Data Query A Cross Cutting Concern?

    Rehein makes the interesting argument that there is no clean separation between the presentation, business, and data access layer. Instead, UI concerns often known only at runtime like paging, filtering, sorting, and so on make data querying a cross cutting concern. He therefore, recommends we stop creating UI interfaces like: Continue reading...

  • Refactor to DataService<T>

    In the post below on NHibernate 101 I said not to plumb data access code in the Operation Contract implementation. Now I'd like to refactor and get that out of there. Let's think about it a bit first. Do I really want to create a data layer class called "InstrumentService" with the CRUD in there? I could. But since this is a concert music application, I know I'm going to have several lookup tables in the system. In addition to musical instruments there will be musical genres, catalogs, recording labels, ensemble types, and so on and on. Continue reading...

  • Brad Abrams on RIA Services with EF

    Abrams basically walked through a typical RIA Services app and most of it is familiar to anyone who has studied the RIA Services documentation and walkthrough. However there were several new things specific to VS2010 and the new bits. Continue reading...

  • WCF 4

    You no longer need to describe in detail all of the configuration for endpoints, bindings and protocols. For example, if you wipe out your web.config then basicHttp protocol is used by default. Also, and this is obscure, by default WCF 4 will act as a protocol bridge. If the client request is SOAP 1.1 but the back end service is SOAP 1.2 the bridge will transform the message automatically, which is transparent both to the client and service. In effect then the client can be an old skool app speaking SOAP 1.1 in http and the back end service can be SOAP 1.2 in tcp and no additional endpoints need to be configured. You do need to create a RoutingService to support routing behavior. Continue reading...

  • VS2010 and MS Deploy

    VS2010 along with the existing project templates for SQL Server 2005/8 will now allow you to configure a complete build package that targets any environment as well as the database. Vishal Joshi did a demo at PDC that was pretty sweet. His blog is http://bit.ly/vijoshi and he has a lot of notes on known issues, release notes, and links to documentation. No longer will it be necessary to get the DBA and the sys admin to coordinate the push to staging or prod. Continue reading...

  • Silverlight 4 Beta Announced

    Just when I was getting used to SL 3, Scott Guthrie announced today at PDC that SL 4 Beta is now available at silverlight.net. Among the new features he demonstrated: Continue reading...

  • Windows 7 Development

    "Building Windows is like building a movie theatre." Microsoft provides the seats, lobby, screen, and so on and we provide the movie. Windows 7 development: research showed that 55% of Windows 7 beta testers had 1024 x 768 screen resolutions. Really? Continue reading...