Blog from Saravanan Arumugam

Let us talk about Technologies

Cancel Validation for a Submit button when jQuery validation is enabled in the page

I have a MVC 3 page where I wanted the unobtrusive validation to happen for my input fields. But on click of one of the buttons I don’t want to trigger the javascript validation.

While going through “jquery.validate.js” for a solution, the solution I found was an easy one.

Here’s the code from jquery.validation.js that gave me the clue.

// allow suppresing validation by adding a cancel class to the submit button
inputsAndButtons.filter(".cancel").click(function () {
	validator.cancelSubmit = true;

So if I simply add the class=“cancel” to a submit button, the validation is ignored for that click.


Working with JSON objects in ASP.NET MVC

In this paper I am going to explain, a simple way of working with javascript, JSON objects to make call to MVC Action through Ajax.


A little introduction to Object representations in Javscript

In javascript, a value to an object can be provided in many ways.

1. Simple assignment

rows = 24;

2. Assignment in Json style

“columns” : “12”

3. Representation of Object

 var newBlog = {blogName: "Working with Json", Content: "….", Index: 51 }

Note that braces represent that it contains an object.

4. Instantiating an Object with “new” keyword

var myString = new String();

5. Representation of Array

var styles = [
{Height:”20”, Width: “50” },
{Height: “30”, Width: “50”},
{Height: “10”, Width: “40”}


Note that the square brackets represent the Array of object/variables.

From javascript object, JSON object can be generated by using JSON.stringify() method. An implementation of JSON.stringify() can be seen in the example at the end of the paper.


Planning the Javascript object for MVC call

MVC includes a default support for JSON binding. When you pass a Json object from client side, the MVC runtime makes all its attempts to understand it, and translate that into a CLR object for you.

For example,

To represent the following class in MVC server side

public class RequestPosition
	public int RequestID { get; set; }
	public int Position { get; set; }

javascript object has to be,

var position = { RequestID : 102, Positon=1 }

Note that the position object has two members and their names (RequestID, Position) are the same as the RequestPosition class’s properties.


To receive this javascript object the MVC action should have the following syntax.

public JsonResult ProcessRequestPosition(RequestPosition position)

Note that the Action has a parameter with the same name as the javascript object we prepared in the client side. When MVC runtime sees a parameter, it looks into the ViewData, query string, Json data etc. to find the value of its parameters.


A complete implementation

Code on the client side

$(document).ready(function () {

	$("#Save").on("click", function (){
		var requestSequence = [{ RequestID: "10", Position: 5 },
			{ RequestID: "11", Position: 8}];
		requestSequence[requestSequence.length] = { RequestID: "12",Position: 9};

		$.ajax("/Queue/SaveRequestSequence", {
			type: "application/json; charset=utf-8",
			dataType: "json",
			success: function (message) { alert(message); },
			error: function () { alert('Oops, the operation failed'); }


Note that requestSequence is an array and is initialized with 2 objects first. Then a third object is added using the index based assignment “requestSequence[requestSequence.length]. requestSequence.length in the place of index ensures that you always add the value at the end of the array.

Here $.ajax does the magic of calling the SaveRequestSequence action from client side.


Code on the server side

	public JsonResult SaveRequestSequence(RequestPosition[] requestSequence)
		//Save Operation
		return Json("Sequence saved successfully!");

Folders created in solution not being deployed

I had some custom folders I created in my MVC3 application, such as Attachments, Forms etc. which I would use from code. For example, one of my web page would receive file attachments (Files being uploaded from the client side) from the user and store it in Attachments folder in the solution.

When I deployed my application, the these folders I didn’t show up on the Web Server.


Then I realized that the visual studio while deployment would not deploy any empty folder. Rather, it deploys files; if the file requires a new folder to be created it would create the folder.

So I created a simple text file in each of these custom folders, say AboutTheFolder.txt. This file ensured that the file gets created after creating the folders I was looking for.

Cannot generate SSPI context


I started receiving the “Cannot generate SSPI context” message recently whenever I run my application from the web server.



It is turned out that this is due to the service account given in the AppPool had undergone a password change.



Look at the AppPool service account and update the password. 🙂

Color alternate rows with different color

Most of us prefer to show a table element with alternate rows showing different background colors.

There has been multiple solutions in the history to do that.

One old way is to apply alternate classes to rows and apply different styles to them. Then ASP.Net form’s grids had property settings for achieving that. There’s a javascript way etc.

I am posting here two simpler and newer ways to do it.


Using CSS3

With CSS 3 doing this is way simple.

    background-color: #eee;

Simple isn’t it.


For the older browsers like IE8, IE7 etc. this may not work. For them, we can use jQuery to do it.

$(“tr:odd”).css(“background-color”, “#eee”);

The code above can do the trick for us.