1

I have a problem with my querystring, i call an action in my controller with javascript like this

if ((event >= 65) && (event <= 90) || (event >= 48) && (event <= 57)) {
    var focussed = document.activeElement.id;
    window.setTimeout(function () {
        alert(in2.value);
        location.href = "/CarSaldi/ListaArt?in1=" + in1.value + "&in2=" + in2.value + "&focus=" + focussed;
    }, 1000);
}

in2 is a input text and might have a "+" inside it (for example "milk+chocolate"), when i call the action in my controller

public ActionResult ListaArt(string in1, string in2, string cod, string focus)
{
    [...]
}

the string in2 shows my "milk chocolate", i expected milk+chocolate...i need to have also the "+" inside.

theLaw
  • 1,261
  • 2
  • 11
  • 23

5 Answers5

4

You should use java script function encodeURIComponent to encode url.

location.href = "/CarSaldi/ListaArt?in1=" + encodeURIComponent(in1.value) 
 + "&in2=" + encodeURIComponent(in2.value) + "&focus=" + encodeURIComponent(focussed);

You need to change you code as follows

if ((event >= 65) && (event <= 90) || (event >= 48) && (event <= 57)) {
var focussed = document.activeElement.id;
window.setTimeout(function () {
    alert(in2.value);
    location.href = "/CarSaldi/ListaArt?in1=" + encodeURIComponent(in1.value) 
 + "&in2=" + encodeURIComponent(in2.value) + "&focus=" + encodeURIComponent(focussed);
  }, 1000);
}
शेखर
  • 17,412
  • 13
  • 61
  • 117
2

Use encodeURIComponent(yourParameter).

In this case special characters are not lost

Shiva Saurabh
  • 1,281
  • 2
  • 25
  • 47
1

Plus sign should be URL encoded in your case:

 +   =  %2B

So instead of using '+' in the URL use '%2B'

See the full table of URL encoded characters: http://www.degraeve.com/reference/urlencoding.php

Andrei
  • 42,814
  • 35
  • 154
  • 218
1

Encode your string using the javascript function encodeURIComponent:

you can use:

 encodeURIComponent(in1.value)
Ranjith Kumar Nagiri
  • 865
  • 3
  • 18
  • 42
rajeemcariazo
  • 2,476
  • 5
  • 36
  • 62
-1

Never, never, never construct your query string in asp.net mvc using javascript and string conatenation. Always let this to Url.Action. The main reason is that what is left on querystring and what is inside the pathinfo depends on how your routes are defined. If you want to give yourself the chance to change your routes easily in the future, use always Url.Action:

var uri = "@Url.Action("ListaArt", CarSaldi", new {in1="xxx", in2="yyy", focus="zzz"})".replace("&amp;","&");
// Replace the "placeholders" values to the real ones
uri = uri.replace("xxx", in1.value);
uri = uri.replace("yyy", in2.value);
uri = uri.replace("yyy", focussed);
location.href = uri;

And regarding the plus sign you need to use encodeURIComponent method in Javascript:

uri = uri.replace("yyy", encodeURIComponent(in2.value));

Hope this helps!

PS: The replace("&amp;","&") is needed because Url.Action generates URLs using & instead of & for separating the tokens of the querystring. This is good for HTML (you can put this in the href of a A tag) but not for the location.href property.

eiximenis
  • 628
  • 4
  • 8