nav-left cat-right
cat-right

ASP.NET MVC + Get Checkbox Value from FormCollecti...

This post will explain how to get the value of a checkbox when a form is posted within the world of ASP.NET MVC. This post will explain this approach by first presenting a common problem and then showing you the recommended solution. You can just jump to the solution by clicking here.

The Common Problem

A lot of times, you will ask a user to “sign up” or “register” to access your web application. Sometimes, this process will involve asking a user to agree to the terms of  the site. This task usually relies on the user checking a checkbox stating that they agree to be a nice user. For instance, you can see an example of one such registration page here.

After a user has agreed, and decided to proceed, you should make sure they checked the checkbox on the server side.  The reason for this is to prevent malicious developers from attacking your site. Regardless, if you are using ASP.NET MVC, you may be surprised to learn that getting the value of a checkbox is a little different from what you may be expecting. For instance, imagine you have the following HTML page defined:

<html xmlns=”http://www.w3.org/1999/xhtml” >
  <head><title>Check Test</title></head>
  <body><form action=”/” method=”post”>
    <%= Html.CheckBox(”chkHuman”, false)%> Are you human?<br />
    <input type=”submit” value=”Move Along” />
  </form></body>
</html>

Now imagine that when the user clicks the “Move Along” button, the following action is triggered:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection formValues)
{
  string value = formValues["chkHuman"];
  return View();
}

If the user checks the checkbox, and this action gets executed, you will notice the “value” variable will be set to “true,false”. If the checkbox was not selected, the “value” variable will simply be “false”.  While you could just check the length, or manually parse the value, there is a more elegant solution.

The Solution

The recommended approach would be to write your code as follows:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection formValues)
{
  bool isChecked = false;
  if (Boolean.TryParse(Request.Form.GetValues(”chkHuman”)[0], out isChecked) == false)
    ModelState.AddModelError(”chkHuman”, “Nice try.”);
  return RedirectToAction(”Index”);
}

In this approach, we are relying on the Request.Form object. By indexing the first element of the value returned by the GetValues method, we will get either “true” or “false”. This can then be easily converted to a bool. The fact that the value can easily be converted to a bool is what makes it the recommended approach.

Mission accomplished! If you would like to learn more about ASP.NET MVC, I recommend checking out ASP.NET MVC in Action.

ASP.NET MVC – Add CSS class Attribute...

If you are a web developer, you probably are used to using the standard “class” attribute in your HTML. If you typically use ASP.NET Web Forms, you are probably used to using the CssClass property on your web server controls. But, if you are new to ASP.NET MVC like me, you may be surprised to learn that the HtmlHelper extension methods do not explicitly provide a parameter for your styling needs.  Instead, if you are using C#, you need to use the object initializer syntax introduced in C# 3.0. To cut to the chase, adding a “class” attribute to an input element generated by a HtmlHelper extension method looks like this:

<%= Html.TextBox(”myTextBox”, null, new{@class = “required”}) %>

Please notice how @class is in bold. The reason for this is because the @ prefix is necessary in this specific case because class is a keyword in C#. If you omit the @ prefix, you will receive a compile time error that says:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1513: } expected

The reasons for wanting to add CSS class information to your HTML elements are numerous. You may simply want to stylize your HTML elements. Or, you may want to prepare your elements to work with a JavaScript framework like JQuery. Regardless of the reason, if you would like to learn more about ASP.MVC, I recommend ASP.NET MVC in Action (or you could just skip MVC and learn Silverlight :) ). As someone that has traditionally written applications using the ASP.NET Web Forms approach I hope to write more about the challenges I come across. What challenges are you experiencing in ASP.NET MVC that you were comfortable overcoming with the ASP.NET Web Forms style?

HTTP Error 500.21 – Internal Server Error Ha...

Are you trying to migrate an existing ASP.NET application to ASP 4 / Visual Studio 2010 and having problems? When I was trying to do this, my ASP.NET application displayed the error listed in the title.  This post will show you how I solved this problem. First though, I want to explain how I got to this point so that you can know if my scenario was the same as yours.

Scenario

Recently, I got around to reformatting my machine. With this reformat, I installed the following main components in the order listed.

  • Windows 7 Ultimate
  • Visual Studio 2010 Beta 2
  • Office 2010 Beta 2

Once those components were installed, I decided to port my code over. I then attempted to run my application. At that point, I received the HTTP Error 500.21 – Internal Server Error Handler “PageHandlerFactory-Integrated” has a bad module “ManagedPipelineHandler” error.

I then verified that I had the .NET 4 Framework installed on my machine. Next I opened IIS 7 and ensured that my application was using the ASP4 application pool (which it was). Needless to say at this point I was confused.

Solution

After trying a variety of things, I decided to just re-install the .NET 4 Framework. I downloaded the framework from here, and ran it. After choosing to repair my installation, and restarting my computer, I found my application successfully working!

« Previous Entries