Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

200
Visualizações
Embedding form values in an ASP.Net MVC JavaScript URL.Action Call

I am trying to pass parameters to an ASP.Net MVC controller from a URL.Action call in JavaScript. I have seen others post things similar to this, but not the exact circumstance I have although I have tried to implement what those solutions were, they did not translate.

I have a button on a modal form that I want to call a method in the controller when it is clicked and the modal form is closed.

Here's the HTML for the button:

    <div class="modal-footer">
        <button type="button" class="btn btn-default closing-modal" data-dismiss="modal">Done</button>
    </div>

here's the javaScript

$(document).on('click', '.closing-modal', function () {
    var datePickers = document.getElementsByClassName("datepicker");
    var times = document.getElementsByClassName("timeField");
    var myStartDate = datePickers[0].value; 
    var myEndDate = datePickers[1].value;
    var myStartTime = times[0].value;
    var myEndTime = times[1].value;

    window.location.href = '@Url.Action("ContractSelectedIndex", "ReviewTicket")';

});

and here's the receiving method in the controller:

    public ActionResult ContractSelectedIndex(string startDate, string startTime, string endDate, string endTime)

I have tried various ways to add a new {}, but because of the @ I can't find the right syntax. This is what I have tried that makes the most sense to me (only first parameter for the sake of the example), but it does not like it.

    window.location.href = '@Url.Action("ContractSelectedIndex", "ReviewTicket", new { startDate: ' + myStartDate + ')' ;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

you can use this if you prefer classic mvc style

var url='@Url.Action("ContractSelectedIndex", "ReviewTicket")'
+ '/' + startDate + '/' + startTime + '/' + endDate + '/' +endTime;

window.location.href = url;

in this case your action should be

[Route("~/ReviewTicket/ContractSelectedIndex/{startDate}/{startTime}/{endDate}/{endTime}")]
public ActionResult ContractSelectedIndex(string startDate, string startTime, string endDate, string endTime)

or this

var url='@Url.Action("ContractSelectedIndex", "ReviewTicket")'
+ '?startDate=' + startDate + '&startTime=' + startTime + '&endDate=' + endDate + '&endTime=' + endTime;

window.location.href = url;

in this case you can use the action you have now

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda