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.

2

5 Answers 5

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);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Use encodeURIComponent(yourParameter).

In this case special characters are not lost

Comments

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

3 Comments

so i can do replace in javascript with %2b
@theLaw Yes, for example
@theLaw better use built-in encodeURIComponent(string) function
1

Encode your string using the javascript function encodeURIComponent:

you can use:

 encodeURIComponent(in1.value)

Comments

-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.