This theme is downloaded from wordpress themes website.

Archive for the 'javascript' Category

jQuery ajax with asp.net mvc preview 5

Quite some had changed between mvc p4 and mvc p5, so it took some time too update my jquery4mvc project to preview 5. Be sure to check out scott’s post to read all the new stuff. He’ll be posting on the ajaxhelpers some time soon also.  If you wish to skip the reading and directly download the stuff: it’s at codeplex.

The javascript-only package includes 3 javascript files that allow you to use the default Microsoft AjaxHelper. You can just copy those files to your Content folder and include them in your head section, instead of the MicrosoftAjax.js and MicrosoftMvcAjax.js. All ajax extensions work the same as their Microsoft equivalence,  using jQuery instead of MicrosoftAjax. As a little bonus, you’ll get back and forward button support for your ajax-links.

image

That’s it, go ahead and use the AjaxHelper functions like you’re used to. Though a few things have changed for the AjaxHelpers in preview 5, you could still use the introduction ASP.NET MVC Preview 4 – Using Ajax and Ajax.Form by Scott Hanselman if you haven’t already checked out the AjaxHelper. Main thing that has changed is that you cannot use inline javascript in the AjaxOptions in preview 5, you should now specify a method name. You could also download the entire jQueryMvc project, it holds a sample website showing some of the ajax magic.

Talking about the entire project, it’s more than just the .js files! The intention of the project is to get you up and running very quickly when you want to create an mvc based ajax application. We use Unity as our default IoC container.

I think it’s quite hard to explain exactly what the project does for you, it’s best really to just look at the sample website, but I will give a brief overview. First, we have a DefaultViewViewEngine. If you mark your controller with a [DefaultView] attribute like so:

image

If you call in example /Home/Index, by default the Index view will be searched for. If it isn’t found, the DefaultViewViewEngine will look for a template with the name specified ("default" in the above example). It doesn’t matter what viewengine you use for your views, it will work for all of them. So what can this do? Look at the structure of the views in the sample site:

image

As you can see, most of the the views only have an ascx file prefixed with an underscore. Since the views are not found when the actions Home/About, Home/SayHello, Home/Index are called upon, the DefaultViewViewEngine will open Shared/Default.aspx.
This default page does not do muchimage

As you can see, it just uses the masterpage. But with only little imagination, you can see that you can easily create different default-pages for each controller. The masterpage however, does hold some extra magic:

image

This special contentplaceholder, by default works like a normal placeholder. But, if it doesn’t get new content from it’s page, it will ask the ViewEngine(s) to open the view of the current action prefixed with an underscore. So, if you call Home/About, this will ask for Home/_About. 
Can you see the magic? For all similar pages, you just have to create the ascx files (or other partials if you use a different viewengine). Those partial views will be pushed into the main page.

But wait, there’s more! If you make an ajax-request to a controller that has the [AjaxController] attribute, it will return only the partial view! Now, isn’t that great ? That looks a lot like the updatepanel to me :)

Go to codeplex and download the project to see the ajax-magic I’m so excited about!
Source can be downloaded at Google code: http://code.google.com/p/jquerymvc/source/checkout
The sample website can be seen in action at: http://www.chrisvandesteeg.nl/demo/jquerymvc5/



kick it on DotNetKicks.com

Ajax & asp.net mvc & c# & javascript Chris van de Steeg 16 Sep 2008 9 Comments

Howto use jQuery with asp.net mvc preview 4’s AjaxHelper

Working on jQuery4mvc preview 4, one of the first things I ofcourse wanted to do was to replace the ASP.NET Ajax library that’s used by the AjaxHelper.

Unfortunately the AjaxHelper itself is sealed, so atm we can’t (easily) change how the AjaxHelper generates its links. So, as an alternative, I created a javascript library that uses the same methodnames as the MicrosoftMvcAjax version.

Though at first I was upset that AjaxHelper wasn’t opened up, this javascript-only solution makes it very easy to use. All you have to do is

  • place the latest jQuery version in your Content folder;
  • place my javascript file in your Content folder;
  • add references in your views (or masterpage ofcourse) to these 2 files instead of referencing the MicrosoftMvcAjax.js and MicrosoftAjax.js file.

If you haven’t played with the AjaxHelper yet, take a look at Scott Hanselman’s post on Using Ajax and Ajax.Form

This jQuery version also has a little extra: browser-history support for your ajax requests

Now, go download the js file and play!



kick it on DotNetKicks.com

.net & Ajax & asp.net mvc & c# & javascript Chris van de Steeg 21 Jul 2008 6 Comments

Ajax and json for ASP.NET MVC with jQuery

I’ve created an example project that uses the jQuery library to create an ajax enabled ASP.NET MVC website. Generally what this project does:

  • Load views through ajax (with back-button support)
  • Do ajax-style form-posts and retrieve only the messages, errors and updated content
  • Do json form-posts and retrieve the errors and messages as a json object

Here are the quicklinks for all you people who don’t want to read the story.

For all you people who want read about it before playing around, here’s a little intro to the code…

The project contains a BaseController, which should be inherited by all your controllers. This controller (ofcourse) overrides the System.Web.Mvc.Controller and handles the detection of Ajax requests and/or Json requests. It does that by overriding the Execute method and reading some http-headers. Unfortunately reading http-headers is the only (evil) way I know to detect this.
Fortunately, while I was playing with this project, I saw that Michael Schwarz posted exactly (well, 99%) the same solution as I figured out, so for now I’ll take this as the best solution until someone provides me a better one.

image

As you can see, there’s also some error handling in here. If there is an exception thrown during the controller execution, it’s wrapped in a simple class called SimpleException. This class is easily serializable, so it can be passed on as a javascript object to the browser, in case of a json request.  In case of a non-json request (either normal or ajax), the default error handler called "error.aspx" in views/shared/ is rendered.

The BaseController also overrides the RenderView. In the RenderView, we do some special stuff if the request is a json- or ajax-request.

image

If the current request is a json-request, the template "json.aspx" is called upon. The default "json.aspx" is located at /views/shared/json.aspx and the only thing it does is serializing the current ViewData.

image

One special note is that in the codebehind, I had ’strong type’ the ViewData to an ‘object’, since the ViewData class doesn’t allow us to serialize the real object.

Then there are the 2 masterpages: ajax.master and site.master. Both of them contain 4 ContentPlaceHolders:

  • Errors
  • Messages
  • maincontent
  • dynamiccontent

Errors and messages should ofcourse output the errors and informational messages for the current request. I created a handy extension method on the HtmlHelper to quickly output these:

image

The maincontent should contain the entire page without the usual MasterPage stuff (eg navigation). The dynamiccontent should only contain the content that can change during the same request (eg. the ‘hello {name}’ part in the demo).

In case of an ajax request, the javascript adds a querystring to the request telling what content is requested (either maincontent or dynamiccontent) and the ajax.master decides what to output for that request based on this querystring:

image

Ajax links are very easily created, also through an extension method:

image

The first parameter is the text of the link, the second one is the action on the controller and the third one is the id of the container-div.

The other client functions are all form related, you an see them in use in the /views/home/index.aspx template:

image

You can use any of these methods to submit your forms, just use them as you like it. The methods can be found in /Content/mvc.js

Well that should be enough talking about code, just go ahead and play to find out the rest by yourself.

  • View online demo
  • Download webproject
  • Download Visual Studio template


  • kick it on DotNetKicks.com

    .net & asp.net mvc & c# & javascript Chris van de Steeg 19 Dec 2007 13 Comments

    Next Page »

    Recommended: Buy movies online.