theLaw
theLaw

Reputation: 1291

MVC ASP.NET querystring incorrect

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.

Upvotes: 1

Views: 973

Answers (5)

rajeemcariazo
rajeemcariazo

Reputation: 2524

Encode your string using the javascript function encodeURIComponent:

you can use:

 encodeURIComponent(in1.value)

Upvotes: 1

eiximenis
eiximenis

Reputation: 638

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.

Upvotes: -1

Shiva Saurabh
Shiva Saurabh

Reputation: 1289

Use encodeURIComponent(yourParameter).

In this case special characters are not lost

Upvotes: 2

शेखर
शेखर

Reputation: 17614

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);
}

Upvotes: 4

Andrei
Andrei

Reputation: 44680

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

Upvotes: 1

Related Questions