Pass jquery parameters to Url.Action ASP.NET MVC

Asked

Viewed 2,277 times

1

I’m having trouble assembling a URL with parameters.

Until then I managed using the @Url.Action, but the second parameter dataPesquisa is going nil.

I imagine when the parameter passed directly to the @Url.Action C# should perform some conversion and in case I am using the replace to be able to give values to the parameters.

Note: I have already checked the variable data and it’s coming with value, it’s only going null when it runs my Action.

Way I did:

var conta = $('#selectContaCorrente').val();
dataParaPesquisa = dataFormatada(dataPesquisa.val());                

var url = '@Url.Action("ExtratoPrint", "Extrato", new { contaCorrente = "paramContaCorrente", dataPesquisa = "paramDataPesquisa" })';

url = url.replace('paramContaCorrente', conta);
url = url.replace('paramDataPesquisa', dataPesquisa.val());

window.open(url, "_blank"); 

Variable value url: /Extrato/ExtratoPrint?contaCorrente=3cb012d1-6ceb-436f-8a0f-22c713703804&dataPesquisa=31/12/2009

Action

public ActionResult ExtratoPrint(string contaCorrente, string dataPesquisa)
{
    ViewBag.ContaCorrente = contaCorrente;
    ViewBag.DataPesquisa = dataPesquisa;

    return View("ExtratoPrint");
}
  • "the dataPesquisa parameter is not going right". What does that mean?

  • No, I just posted my action

  • "No" what? I asked what that means.

  • @LINQ, sorry, I edited my question, I think it’s now clearer.

  • Checks whether the variable conta has some value when calling that script there.

  • I already checked, in my action when called the account has value, but the dataPesquisa comes null, I will post my variable url to check how it is getting.

  • @LINQ, take a look

  • How do you plan to use a bar date if bar is just a "reserved character" in URL’s?

  • So, I figured that would be the problem, but my question is, how to pass parameters to the Url.Action with jquery variables, because I imagine that putting the data parameter, c# does the conversion, you know ? so much so that I used these parameters in $.getJson calls and it worked. Now that I’m using with the Url.Action I’m having trouble.

  • There’s no way you can do that. Url.Action is Razor code, it will run on server, the Javascript code runs on the client. You may notice this by opening the code on your page, in the script part there will be no Url.Action, there will be only the value returned by this function. This is because it has already been executed on the server.

  • @LINQ, do you know if there’s any other way I can call my action and open it in a new window the way I’m doing it ?

  • There are a million ways. Why don’t you just change the date format before making the request? Make your URL look like this dataPesquisa=31-12-2009.

  • For what format for example? could give me a horizon, rsrs.

  • Dude, I wrote the format in the comment.

  • I put it the way you said it and keep going null the parameter :/

Show 10 more comments

2 answers

2

I solved my problem using @Html.Raw, as follows:

var url = '@Html.Raw(@Url.Action("ExtratoPrint", "Extrato", new { contaCorrente = "paramContaCorrente", dataPesquisa = "paramDataPesquisa" }))';

0

You can concatenate the values, something like this:

var conta = "3cb012d1-6ceb-436f-8a0f-22c713703804";
var dataPesquisa = "31/12/2009";
var url = '@Url.Action("ExtratoPrint", "Extrato")?contaCorrente=' + conta + '&dataPesquisa=' + dataPesquisa;

The result will be this:

/extrato/extratoprint?contaCorrente=3cb012d1-6ceb-436f-8a0f-22c713703804&dataPesquisa=31/12/2009

Browser other questions tagged

You are not signed in. Login or sign up in order to post.