0

I have an error in MVC, when I pass as parameter '&' fails to come the following. How will I show in this example?

public IActionResult Create(int? idTrabalhador, [FromQuery]string GridState{}

view

var lnk = "@Url.Action("Create" , new { idTrabalhador = "{#KeyTrabalhador}", GridState = "{#GRID_STATE}" })";
    lnk = lnk.replace(encodeURIComponent("{#KeyTrabalhador}"), 0);
    lnk = lnk.replace(encodeURIComponent("{#GRID_STATE}"), JSON.stringify(state));

http://localhost:58185/TrabalhadorPincodes/Create?idTrabalhador=0&GridState={"filter":{"NomeTrabalhador":"nuno","Estado":"0"},"sorter":[],"currentPage":0}

model => GridState = "{\"filter\":{\"NomeTrabalhador\":\"nuno\",\"Estado\":\"0\"},\"sorter\":[],\"currentPage\":0}"

Perfect!!!

BUT

http://localhost:58185/TrabalhadorPincodes/Create?idTrabalhador=0&GridState={"filter":{"NomeTrabalhador":"&","Estado":"0"},"sorter":[],"currentPage":0}

model => GridState = "{\"filter\":{\"NomeTrabalhador\":\""

need help!!!

Nuno Malés
  • 123
  • 1
  • 8

2 Answers2

0

The browser is interpreting the & as part of the url as you are sending the http request via the GET method.

One method to handle this is to urlEncode your parameters using Server.UrlEncode This also means you will need to decode the parameters within your controller.

The other solution is to post the values so that they are not part of the url but you will need to decide whether this is appropriate.

hairmot
  • 2,975
  • 1
  • 13
  • 26
  • ow to do this, I've tried and it's still the same lnk = encodeURI (lnk); – Nuno Malés Feb 28 '18 at 10:55
  • please see this question and assosciated answer: https://stackoverflow.com/questions/561954/asp-net-urlencode-ampersand-for-use-in-query-string – hairmot Feb 28 '18 at 11:04
0

because the 'encodeURIComponent' only works in plain text, what I'm doing is creating a compound object soon does not work. I have to use the 'encodeURIComponent' as soon as the object is constructed not after it is built

filter.NomeTrabalhador = encodeURIComponent($('#NomeTrabalhador').val());

Nuno Malés
  • 123
  • 1
  • 8