Things I learned about ASP.NET MVC 2 at the jQuery Conference

I’m at the jQuery Conference in Cambridge, MA this weekend. Today was day one of two and it was a great start to the conference. Stephen Walther from the ASP.NET team at Microsoft gave a talk on building apps with ASP.NET and jQuery. The talk got very interesting when he mentioned that he was running unreleased bits from a recent internal build. There were two things that were previously unannounced that I found interesting.

The first thing Stephen demo’d was using the jQuery Validation plugin with MVC. It’s been publicized that Microsoft is planning to ship the Validation plug-in similar to how jQuery core is included. What hasn’t been shown publically (to my knowledge) were any code examples. The one that caught my eye the most was this snippet which was placed just under the opening <body> tag in the view (from memory, exact syntax may vary):

    <% Html.EnableClientValidation(); %>

After the talk Stephen let me view source on the outputted page. This helper method outputs the JSON structures needed for the validation plugin to work. Interestingly, it outputs them at the end of the page just before the closing body tag. Does this remind you of anything? Reminds me of Webforms. Declare something one place in your code and have it do things somewhere else. The other thing I don’t like about this syntax is the name of the method. I don’t think that’s a very descriptive term for what it’s actually doing. I’m working on limited information here though so it might be doing more than just outputting the JSON data. It’s still only pre-Preview 2 and MVC 1 went through 5 previews and a couple RCs with lots of refinements along the way so it could very well change.

The second interesting thing that was discussed was a change in the way GET requests for JSON are going to be handled. The reasons for this have been covered extensively. If you want to allow JSON to be returned via a GET request, you’re going to have to be more explicit about it before you risk shooting yourself in the foot. While the sentiment behind the decision is good, I’m not a fan of the current way you have to specify it (again, from memory):

    public ActionResult GetJson() {
       return new JsonResult(data, JavascriptBehavior.AllowGet);
    }

What I deduce from this is that by default JsonResult won’t return data if the request is a GET without explicitly allowing it. This change would break existing code since it would need to be updated to allow the GETs. If the default is going to be changed though, why add the parameter? Why not just check to make sure the proper AcceptVerbs attribute values are present? Again it’s early and could change even before the next preview, but it’s a curious change.

From my experience it’s rare when MS demos something at a conference that wasn’t already covered in some other form (usually blog posts) so it was an exciting afternoon to get a little advance preview of the changes coming in MVC 2. Even though these two features aren’t implemented the way I would like, the MVC team has proven that they’re listening to feedback and willing to make changes along the way if better ideas are presented. That sort of openness into the process is reassuring and I’m appreciative of the team’s efforts.

Tomorrow there will be an open spaces meeting for ASP.NET folks that use jQuery. I’m looking forward to sharing some ideas and helping some others out if they have any questions that I can answer. If anything interesting comes out of it, I’ll be sure to pass it on. Also, be sure to follow me on Twitter if you’re interested in my thoughts as the conference goes on tomorrow.

Posted September 12th, 2009 10:01 PM
Read more posts about ASP.NET MVC.

View Comments
Link

  • Scott & Kazi,
    You are correct about the placement and I should have mentioned that I was aware of the performance reasons for putting it there. I don't have any issue with the placement in itself, other than the HtmlHelper that's putting it there is not in the same place as the code. This signals to me that they're going down the road of starting to abstract away what's going on under the covers and I don't want to go down that road again. It's not a big deal though, just something I found curious. With a more descriptive name of what the method is doing, I'm on board.
  • 1. I think it is one of the best practise as per YSlow to put your JS at the bottom, I do not see any issue over here.

    2. Not Sure why they need this extra parameter, There is already AcceptVerbs and Route constraint to handle tis.
  • RE: putting the JSON output for the client validation at the end of the page - There are a few technical and performance reasons for doing that. Moving you JS to the bottom of the page allows all of the elements to render before the script is executed. Which means that 1) the page appears faster to the user 2) the elements are present before any JS runs so that events can be attached to them. (although this really isn't necessary with most modern JS libraries as most of the provide some kind of DOM-Ready event for you to handle.)
blog comments powered by Disqus