Tuesday, 5 July 2011

Monday, 4 July 2011

Syntax highlighting in Blogger

I have started using this syntax highlighter on Blogger! It's great!

SyntaxHighlighter

How to use SyntaxHighlighter in Blogger?

Simple jQuery AJAX example calling a MVC controller

If you can't see the syntax highlighting in your feed reader (i.e. Google reader) please open the link in your browser.

In this post I will outline a very simple AJAX call to an MVC controller object using jQuery and JSON.

Lets start with the front-end code. Below is a simple HTML input and anchor tag:



Find


When a user enters an image ID in the in the text box and clicks on the find link we will send an AJAX request using jQuery and sending JSON data to the MVC controller. The JSON helper object below is explained here. Below is the JavaScript code:

$(".findLink").click(onFindClick);

function onFindClick(e) {
var jsonData = { imageId: escape($("#ImageId").val()) };

$.ajax({
        type: "POST",
        url: "/Controllers/ImageManager/FindImage",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(jsonData), // Convert JavaScript object into a JSON object string.
        dataType: "text json",
        success: function (XMLHttpRequest, textStatus, data) {
            var jsObj = jQuery.parseJSON(data.responseText); // Convert JSON object into a JavaScript object.

            if (jsObj != null) {
                alert("JSON result = " + data.responseText);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("There has been an error: " + errorThrown + " - " + textStatus + " - " + XMLHttpRequest.statusText);
        }
    });
}


Now for the MVC controller:

public class ImageManagerController : Controller
    {
        public string FindImage(string imageId)
        {
            // Do some code to get the image object...
// Assume image object has 2 properties: Id and Url.
            return SerializeNode(image);            
        }
}

        private static string SerializeNode(ImageReference imageRef)
        {
            var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            var simple = new { Exists = imageRef != null, Id = imageRef != null ? imageRef.id : 0, Url = imageRef != null ? imageRef.Url : "" };
            return serializer.Serialize(simple);
        }


Hope this helps. A very brief example but should outline the major points.

Friday, 1 July 2011

Cross-browser JSON Serialization in JavaScript

This is a great site that helps serialize JSON to string and to a JavaScript object.

Link
http://www.sitepoint.com/javascript-json-serialization/

Thursday, 9 June 2011

Could not load file or assembly 'XXXXX' or one of its dependencies. An attempt was made to load a program with an incorrect format.

This started happening all of a sudden when I tried to build or rebuild my VS2010 solution. This was because some of the projects in the solution target different platforms (mixed platforms).

I fixed this by going to the Configuration Manager... which is in the drop-down of your configurations (Debug, Release etc.) in VS2010. Then I set "Active solution platform" drop-down to "Any CPU".

I rebuilt the solution and it succeed in compiling.

Tuesday, 7 June 2011

Host file not working

I had my website folder e.g. C:\www\wesbite1
I created an IIS 7.5 website e.g. Website1 with the host name = website1.dev
I pinged website1.dev and received a reply

Then when I goto http://website1.dev I get an error:

Cannot find server - DNS fail

What the hell?

Then I remembered that I'm using a proxy and I had to ignore website1.dev in the proxy settings of my browser. This can be found in Firefox > Options > Advanced > Network > Connection Settings > No proxy for:

Hope this helps.

Wednesday, 13 April 2011

Html form not submitting values to server code c#

If you have a HTML form like this within a .NET master page and you not getting values make sure you have a name attribute for each control. I had an id attribute and this was not submitting. Correct form below:

<input name="search" type="text">
<input type="submit" value="Submit">