• Empleos
  • Bootcamp
  • Sobre nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Publicar empleo
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

111
Vistas
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 + ')' ;
over 1 year ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

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

over 1 year ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos