Contents tagged with MVC

  • Polling with Knockout, JQuery, AJAX, and MVC

    Some colleagues and I have been taking a close look at Knockout, the javascript library that implements the MVVM pattern for rich client UI. Knockout takes advantage of the new custom data attributes (data-*) in HTML 5 to bind declaratively a javascript view model to UI elements. That's pretty cool. But even better, Knockout will update your UI automatically whenever the underlying view model changes. It does this when you declare your model as an observable: Continue reading...

  • 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

    RSS RSS

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