Best way to escape characters before jquery post ASP.NET MVC

Posted by Darcy on Stack Overflow See other posts from Stack Overflow or by Darcy
Published on 2010-02-19T16:40:35Z Indexed on 2010/03/18 2:01 UTC
Read the original article Hit count: 728

Filed under:
|
|

Hello, I am semi-new to ASP.NET MVC. I am building an app that is used internally for my company.

The scenario is this: There are two Html.Listbox's. One has all database information, and the other is initally empty. The user would add items from the database listbox to the empty listbox.

Every time the user adds a command, I call a js function that calls an ActionResult "AddCommand" in my EditController. In the controller, the selected items that are added are saved to another database table.

Here is the code (this gets called every time an item is added):

function Add(listbox) { ...
//skipping initializing code for berevity

var url = "/Edit/AddCommand/" + cmd;

$.post(url);

}

So the problem occurs when the 'cmd' is an item that has a '/', ':', '%', '?', etc (some kind of special character)

So what I'm wondering is, what's the best way to escape these characters? Right now I'm checking the database's listbox item's text, and rebuilding the string, then in the Controller, I'm taking that built string and turning it back into its original state.

So for example, if the item they are adding is 'Cats/Dogs', I am posting 'Cats[SLASH]Dogs' to the controller, and in the controller changing it back to 'Cats/Dogs'.

Obviously this is a horrible hack, so I must be missing something. Any help would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about mvc

Related posts about JavaScript