Taswar Bhatti
The synonyms of software simplicity
linq

You are here beacause you probably got a Linq error, specifically Operator ‘==’ cannot be applied to operands of type ‘System.Collections.Generic.KeyValuePair‘ and ‘
So if you wish to do something like this to KeyValuePair using Linq

You will get this error
Operator ‘!=’ cannot be applied to operands of type ‘System.Collections.Generic.KeyValuePair‘ and ‘

Since by default KeyValuePairs are struct.
To solve the issue we need to try out this

Hope this helps 🙂

jquery

Was coding some new functionality and though I would post this, so that others can find it helpful.
Lets say you have a form that has a checkbox that will update the select dropdown box with different values, when the checkbox is clicked.
Something like the UI below.

Your html code might look like

On your server side you would provide a Action Method to filter out the data, something like

Now we got to add the javascript to listen to the checkbox event changed, something like this in jquery, since we are getting Json Result we will have to parse the json object to populate the dropdown box.

By doing so you will have something that shakes and updates the drop down box by some simple ajax calls 🙂

So haven’t posted for a while, was in home city Hong Kong for holidays and busy with work releasing software, gonna be posting a few more things coming up this month so be tuned in 🙂

Javascript

So was going through some legacy code to fix some security issues. One of them was there were links that were passing the data on url request. e.g NewFile.aspx?uid=1234

Rather than storing data in a session sometimes developers use shortcuts to do this, could be due to the pressure or time limit we have in shipping a product.

Aside from that lets see how we can fix this issue, what we want to accomplish is to post some data without calling server code and we can achieve that by some tricks in javascript.

Lets say you have a link that will say <a onclick=”javascript:NewFile()”>New File</a>
(Note I know this is not good again its legacy code)

Now we want to make a post request to the window and pass in the data.
Here is how we do it, I created a blank html page first and used this javascript.

By doing so we can pass in the data to the NewFile.aspx page with a post request now, also note if you are using Request.QueryString[‘uid’] in the NewFile.aspx page you will need to change it to Request[‘uid’]

Hope this helps 🙂

serviceLocator

There are certainly many ways to stub out dependency, using constructor injection, etc etc. Here is one way to quickly stub out ServiceLocator rather than implementing ServiceLocatorStub.

Lets say you have code that uses a helper and the dependency is not quite the responsibility of that object but its the sub object that uses it. As an example the IAccessHelper uses some security server to check some values. But the AccountController doesnt use it so why inject security service in AccountController? In that case you will call a service locator to get you the AccessHelper object.

Now if you wish to unit test this you will need to do quite a few things to stub it out. The quick way to do this is using property in C#

So that now when you write your unit test you dont have to care about service locator you can just stub out your IAccessHelper by just passing in a mock or stub to it.

This helps one to quickly break out the dependency of an object, that would otherwise take too many lines of code to test out a simple idea.

json

I found this weird error when I was testing a no trivial test case in my app and was not able to find out what was wrong with it.
It appears like a Json error in my asp mvc app, although the stack really doesn’t help 🙂


Basically what is happening is Json does not like to have circular reference ie. A -> B -> A
The easiest fix is to use [ScriptIgnore] in your ViewModel to tell it not to serialize object that may cause such a circulate issue.

Add the System.web.extensions reference to your viewmodel and make the elements that may cuase issue to scriptIgnore

example

By doing so now your json would look more like { Id: 3, FirstName: ‘Taswar’ }.
Also there is JSON.NET that provides [JsonIgnore] attribute if you are using json .net

jquery

So here is an interesting jquery plugin called jqprint it allows one to specify any element and sent it to print.

Assuming you have an element with id printButton and a content area called divToPrint

This will pop up the print functionality in your browser and send it with the default media=”print” css or else it picks up your default css. Kind of neat.

One thing to be mindful of in Firefox is the overflow attribute in your css stylesheet if you have overflow: hidden you will only get one page to print only.

Example:

Remove the overflow and it would print all the pages of a div.

jquery

So for some reason IE just loves to cache things for you when you call an ajax method, thus my JQuery get ajax call not working in IE.

Something like

This will work every time in firefox but IE will just call the cache version, thus whatever you have changed in the post version would not matter.

To fix this one can use datetime to append to the query string in each call or just use jquery ajaxSetup like

or use the $.ajax call and pass in cache: false.

jquery

So was having issues as in why does my checkbox doesn’t work in IE when I do

And found out that jquery does not bind live “change” events, I was told that it works in 1.4.2 but for some reason I still was not able to get it working.

The solution that I found was to use the livequery or bind method thus the code would become

or if you are using Ajax then

That should do the trick, remember to add jquery.livequery.js if you are using livequery in your file.

UA-4524639-2