I had an asp.net-mvc site and I have an issue where I had an "&" in my query string that I am building with javascript and then using ajax to my server. Something like this:
www.mysite.com/MyController/Load?Tag=M&Ms
when I passed this to my server using ajax, it was showing up as
Tag="M"
so, in my javascript code, I changed my code from this:
"Load?Tag=" + tag;
to:
"Load?Tag=" + encodeURIComponent(tag);
and this fixed my issue. My server code looks like this
public ActionResult Load(ProjectParams projectParams)
{
//go do stuff
}
where ProjectParams looks like this:
public class ProjectParams
{
public string Tag {get;set;}
}
So everything was great until I realized that making this change on the javascript side, had a knockon effect. I just ran into an issue where if I have spaces in my tag name, it shows up on the server as:
My%20Tag%20Name
instead of:
My Tag Name
what is the correct way to encode on the javascript side for a url querystring and ensure that it shows up properly on the serverside controller action