Sharing my knowledge

JQueryUI Datepicker in ASP.Net MVC

Datepicker is nice and cool plugin for displaying the calendar with ease. It is very easy to use JQuery plugin, it comes as part of JQueryUI library, so if you want to use this – first download JQueryUI from http://jqueryui.com/download and also download JQuery(http://docs.jquery.com/Downloading_jQuery) if you haven’t done yet. For e.g. if you have a form like one below

<% using(Html.BeginForm()){%>
    <legend>Event Information</legend>
        <label for="EventName">Event Name:</label>
        <%= Html.TextBox("EventName")%>
            <label for="StartDate">Start Date:</label>
            <%= Html.TextBox("StartDate")%>
            <label for="EndDate">End Date:</label>
            <%= Html.TextBox("EndDate")%>
                <input type="submit" value="Save" />
<% }%>

and you want to attach datepicker to “StartDate” and “EndDate” input fields,


Logging execution time using AOP

Download the source code for this tutorial from  - http://www.rajeeshcv.com/download/ProfilingSample.zip

What happens if your client complains that your application is running very slow!!! or in your load/stress testing you found that some functionalities are very slow in executing than expected. This is the time where you go for profiling the execution, to analyse the root cause of these issues.

So how we could develop a profiler, where we don’t have to wrap our normal code in a profiling code.

Before going to create the profiler, we have to decide where to put the profiled information. In this tutorial, I am making use of Log4Net as underlying layer to store this information. If you have not used Log4Net before, I suggest you to read http://www.beefycode.com/post/Log4Net-Tutorial-pt-1-Getting-Started.aspx as a starting point.

With the help of AOP (Aspect-Oriented Programming) we could do the profiling task without interrupting the actual code.

AOP is a programming paradigm in which secondary or supporting functions are isolated from the main program's business logic

Source : Wikipedia

So in order bring the AOP functionality into this application, I am going to use a third party library PostSharp  which I believe this is one of the best that is available in the market. Please download it from http://www.sharpcrafters.com/postsharp/download.

So, now we have got the basic things to start with and now let’s start coding….

Start a new solution in visual studio and add a new console application project to it. Then add the below references to the newly created project

  1. Add reference to the Log4Net.dll
  2. Add reference to PostSharp.Laos.dll and PostSharp.Public.dll (Please read http://www.sharpcrafters.com/postsharp/documentation/getting-started to get the basic installation procedure)

Next, create a new attribute class called “ProfileMethodAttribute” – this class is responsible for doing the profiling work. Make sure that you have decorated this class with “Serializable” attribute


Check out Save Our Tigers | Join the Roar

Save Our Tigers | Join the Roar


Our national animal is fighting for its life. From around 40,000 at the turn of the last century, there are just 1411 tigers left in India.

If we don’t act now, we could lose this part of our heritage forever.
Speak up, blog, share the concern, stay informed… Every little bit helps.

ASP.Net MVC – Conditional rendering Partial Views with Action<T> delegate

This is an update to my previous post regarding conditional rendering partial views, in that I used the internal implementation of the Html.RenderPartail(…) method to create the Html extension. Later I found a simple way to achieve the same using Action<T> delegate

<p>Partial rendering with Action Methods</p>        
<% Html.PartialIf(this.Model.Exists, html => html.RenderPartial("MyPartialView")); %>

If you look at the “PartialIf” implementation, it is simple, cleaner than the previous technique I have mentioned in my post.

public static void PartialIf(this HtmlHelper htmlHelper, bool condition, Action<HtmlHelper> action)
    if (condition)

That’s it :)

ASP.Net MVC - Conditional rendering Partial Views

Update : Later I found a cleaner and simple approach to do the same – read this post ASP.Net MVC – Conditional rendering Partial Views with Action<T> delegate

Following my previous post about Conditional Rendering, one of my colleague asked me how to render the partial view based on a condition.

Normal way of doing this is

<p>Normal partial rendering based on condition</p>           
<% if(this.Model.Exists)
 } %>

I am not sure about any other technique for rendering partial view conditionally other than this (correct me if I am wrong :) ).

Then I thought about copying the pattern I have used in my previous post and came up with this code which could conditionally render partial views and you could use the Html extension like below, which more clean than the previous

<% Html.PartialIf(this.Model.Exists, "MyPartialView"); %>


ASP.Net MVC - Conditional Rendering

We come across situations like rendering elements based on the conditions in Model. For example, in the view if we want to show a TextBox if some property is true. A normal way of doing this is like below

<% if (this.Model.Exists)
      <%= Html.TextBox("Test") %>
   <% } 

This looks like old classic asp style, and when it comes to code maintenance this will be a pain. So a clean way is to use an Html helper to generate this

  <%= Html.If(this.Model.Exists, action => action.TextBox("Name")) %>

which looks cleaner than the old one. Source code for this helper method is

public static string If(this HtmlHelper htmlHelper, bool condition, Func<HtmlHelper, string> action)
    if (condition)
        return action.Invoke(htmlHelper);

    return string.Empty;

What about IfElse condition, we could write another helper method for that


FluentConfigurationException: An invalid or incomplete configuration was used while creating Session Factory. Check Pot

I got this exception when I tried to run my Repository tests and couldn't find the exact reason why these tests failed.

All the unit tests where passing before I removed the default constructors from Domain Model, when I reverted the default constructors back, everything started working again..... !!!

Do you know the exact reason for this?

Comet a.k.a server pushing

Have you ever thought about how chatting in Gmail works? I think it works using a programming technique called “Comet”.

What is comet programming

In web development, Comet is a neologism to describe a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it. Comet is an umbrella term for multiple techniques for achieving this interaction. All these methods rely on features included by default in browsers, such as JavaScript, rather than on non-default plugins - – Wikipedia

Last few days, I was reading about this technology and thought about sharing the information with you all. Please find the attached sample application which demonstrates how comet works using Asp.Net (Note: Right now it works only in firefox, fixes or patches to make it work in IE/Safari.. or all the browsers in this world are welcome J )

Please download it from here - http://www.rajeeshcv.com/download/Comet.zip

Tip for writing better NUnit test cases

If you have method, which does the string reverse. So normally we writes test cases in Nunit something like this

public void TestCase1()
ExString exString = new ExString();
Assert.AreEqual("dcba", exString.Reverse("abcd"));

public void TestCase2()
ExString exString = new ExString();
Assert.AreEqual("abba", exString.Reverse("abba"));

This means, you are repeating the same code again and again in order to test different possibilities. The down side of this approach is, in long run it will be very difficult to manage the test case when compared to the actual code.

To solve this particular problem NUnit has got parameterised test cases attribute, so the same could be re write above test case to

[TestCase("abcd", "dcba", TestName = "Test Case 1")]
[TestCase("abcd1", "1dcba", TestName = "Test Case 2")]
[TestCase("aaaa", "aaaa", TestName = "Test Case 3")]
[TestCase("abba", "abba", TestName = "Test Case 4")]
public void Can_Reverse_A_Text(string actual, string expected)
ExString exString = new ExString();
Assert.AreEqual(expected, exString.Reverse(actual));

This gives a clean way for testing different possibilities, all these shown in the NUnit test runner as different test cases like below.


Hope this will help in some way or other