Archives

Archives / 2011 / June
  • Snuggle up to jquery $.ajax

    In an earlier post I described how to use the MVC 3 Ajax.ActionLink to post an async delete back to the server. Ajax.ActionLink depends upon jquery.unobtrusive-ajax.js, a script included with the MVC 3 project template. As Brad Wilson has blogged, and I demonstrated, you create an AjaxOptions class and pass it into the ActionLink method constructor. When the view is rendered those properties are emitted with the anchor tag as HTML 5 data-* attributes: Continue reading...

  • 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...