This week I decided to take a legacy web application that I wrote in ASP.NET and convert it to MVC. I started the process by reviewing the NerdDinner source code and by glancing through the free MVC e-book. Alternatively, you can just jump in head-first and have Google on hot standby for when you get stuck.

For background, my application is a search engine which allows users in my organization to find servers, instances, databases and etcetera. The main page of the application looks a lot like Google. The search interface has a basic and simple mode. The advanced mode allows you to specify which attributes are searched, the matching style ( starts with, contains etc. ) and which kind of results are returned. When the form is submitted I check to see which options are checked and I pass them into a stored procedure using LINQ to SQL. On this form I have three different categories, which are attributes, styles and results types. The attributes and result types can have multiple items selected and the search style is a single selection. Now, since I did not want to have a zillion if statements sprinkled in my code looking which options are checked I used the concept of search flags for the attributes and result types. Basically what I ended up with was HTML that looked like this:

You will notice that each checkbox has a unique value assigned to it and the sum of each combination will always generate a unique number. For example, if I select Server (value =1 ) and Instance ( value = 2 ) I will get a sum of 3. If I select Server and Purchase Order I get a value of 129. Great, this makes things simple, so now all I have to do is submit the form and check the values in the controller to see which options are checked. Since the checkboxes are all prefixed with the string “attr$†in the name I can loop over the Request variables and generate a sum. Unfortunately when I inspected the values for each checkbox I noticed that the results were very strange. They looked like this:
Request["attr$1"] = {1, "false"}
Request["attr$2"] = {2, "false"}
Request["attr$3"] = {"false"}
Request["attr$4"] = {"8,false"}
Request["attr$5"] = {"false"}
Sometimes I got numbers and booleans, other times I got just a number. The most interesting part was that the value returned was not dependent on the checkbox state (checked or un-checked). Unfortunately, this meant that I could not rely on checking the individual checkbox values on the server side. Therefore I had to resort to some good ol' fashioned JavaScript. What I ended up doing was creating a two hidden fields on my form. When you check/un-check options the JavaScript function stores the running sum of each search category into a hidden field:
function updateFlags(checkbox) {
var ctrl;
if (checkbox.name.substring(4, 0) == "ret$")
ctrl = document.forms[0].resultTypes;
if (checkbox.name.substring(5, 0) == "attr$")
ctrl = document.forms[0].attributes;
if (checkbox.checked == true)
ctrl.value = parseInt(ctrl.value) + parseInt(checkbox.value );
else
ctrl.value = parseInt(ctrl.value) - parseInt(checkbox.value);
}
Now, in the controller I can get the value of the hidden fields and pass them as parameters to my stored procedure. This may not be the most graceful solution but considering the circumstances I am rather pleased with the end result.