ASP.NET MVC 2 and MongoDb Part 4 – First steps with jQuery, AJAX and JSON

In the past articles we created a simple application to submit bugs to a project. If you run the BugBase app, it looks like this:

It’s ugly!
Let’s add some style quickly. And since I am not a webdesigner, I Google for ‘minimalist templates’ and grab a free one, like this one, courtesy to Cardeo.

Masterpages are around for eons, so I won’t go into the technical details of this phenonemon much. After creating a masterpage from Cardeo’s design, BugBase looks like this:

And the bug submitform:

It’s slightly better, no?

JavaScript, Ajax and JSON

Ajax is a technique that provide a snappier user experience through the use lightweight calls to the web server, in stead of full roundtrips to the server every time you submit a form. These asynchronous calls are initiated on the client using JavaScript and involve formatting data, sending it to a web server, and parsing and working with the returned data. While most browsers can construct, send, and parse XML, JavaScript Object Notation (or JSON) provides a standardized data exchange format that is better-suited for Ajax-style web applications. (Okay, I didn’t write the last paragraph myself, Microsoft did. But I never found a more clever desription of the JQuery. AJAX and JSON triade.)

So, let’s Ajaxify the Bug submit form. Let’s make use of the jQuery Form Plugin, which actually does everything for you. Just copy jquery.form.js to the Scripts folder and make references in the head sections of Create.aspx (or in the BugBase.Master).

Next, I’m going to go ahead and add my own .js file (Add, New Item) to the scripts folder for my jQuery code. Also add a reference to this script:

[csharp htmlscript=”true” highlight=”4,5,6″]
<title>BugBase the Bug submitting app</title>
<link href="../../Content/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript" src="../../Scripts/jquery.form.js"></script>
<script type="text/javascript" src="../../Scripts/BugBase.js"></script>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

Next thing we need to do, is add an ID to the /Bug/Create form. As you can see, the HTML is rendered with the help of HTML Helpers (hm, so that’s where those helpers get their names from) but I am not sure if HTML is better with or without these helpers. So, I just click on View Source in my browser and copy the generated HTML to Create.aspx. Next, I can add an ID to the form, which is important because this is how the jquery.form.js references the submit form.

[csharp language=”true” highlight=”1″]
<form action="/Bug/Create" method="post" id="BugSubmitform">
<div class="editor-label">
<label for="Name">Name</label>
<div class="editor-field">
<input id="Name" name="Name" type="text" value="" />
<div class="editor-label">
<label for="Description">Description</label>
<div class="editor-field">
<textarea cols="20" id="Description" name="Description" rows="2">
<input type="submit" value="Create" />

Next step is edit the (still empty) BugBase.js and add the following code:

[javascript highlight=”5″]
$(document).ready(function () {
dataType: ‘json’,
success: function (response) {
$(‘h6’).text("Bugname: " + response.Name + ".
And the description is: " + response.Description);

Hmm, this is pretty self-explanatory I think? The first line you should know by heart if you plan to write more jQuery.
The next thing you see how we reference the id of the form, i.c. #BugSubmitform. So the form plugin knows at which point it should wake up. Then we are specifying the dataType, which is json. On success, we’re going to display an alert, with the Description of our bug. Also, we will display the bug name and bug description between H6 tags, which I added on the Create.aspx page.

You see, ‘response’ in line 5 is actually the submitted bug serialized in json format. So, we can use all the properties of the Bug class here!

We’re not completely finished though. We’ll have to make sure the Create function of our controller actually returns JSON. Which is in fact very easy:

public ActionResult Create(Bug bug)

if (Request.IsAjaxRequest())
return Json(bug);

return new RedirectResult("Create");

That’s it. Let’s try it.

To see that we really really Ajaxified things, we should fire Firebug form Firefox. If you check out the console, you’ll notice the XMLHttpRequest, which proves that this was an AJAX request and the returnformat is JSON:

So now we know how we can speed up our app by using AJAX and jQuery and how we can provide the user feedback with the JSON result. As soon as I completed the BugBase app, I will post it here for you to download.

Entity Framework from scratch Part 4 EF4 Multi Layered – System.Data.Entity not referenced

As i mentioned in the previous post, there seems to be a problem when you work with Multi layered Solutions in the Entity Framework. You need to add the Entity references to all the solutions in your project, but i found another small problem when creating a web front to your application.
I’ve built this app with EF4 features with web functionality.

When i debug my code, the database connection works and the table rows are fetched, but my webpage displays an compilation error: CS0012: The type ‘System.Data.Objects.DataClasses.EntityObject’ is defined in an assembly that is not referenced. You must add a reference assembly to ‘System.Data.Entity, version=, Culture=neutral, PublicKeyToken=b77a5c561934e089’.

You need to add extra code manualy to your Web.config file directly under the system.web


copy/paste the following code:

<compilation debug="true" targetFramework="4.0">
<add assembly="System.Web.Abstractions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Data.Entity, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

If you built and re-run the the app, the webpage is displayed correctly.