How to pass two parameters with AJAX and receive them in the controller?

1

How can I pass two parameters with ajax and receive them in the Controller as parameters

public IHttpActionResult GetComplementoMateriaId(int id, int IdUnidad)

Update:

Well, it's actually a GET what I want to do, I work with pages .html, so what I do is that from a jqGrid table I select an element and pass the id of that element through the url, and what I'm trying to do is pass two variables by url or if you can pass from AJAX similar to what you sent me, and receive them in the controller something like this

[ResponseType(typeof(COMPLEMENTOMATERIA))]
public IHttpActionResult GetComplementoMateriaId(int id, int IdUnidad){
    try{
        List<COMPLEMENTOMATERIA> lstComplementoMateria = LGComplementoMateria.GetComplementoMateria();
        List<COMPLEMENTOMATERIA> ComplementoMateria = lstComplementoMateria.FindAll(x => x.IdMateria == id);
        List<COMPLEMENTOMATERIA> Complemento = ComplementoMateria.FindAll(x => x.IdUnidad == IdUnidad);

        if (ComplementoMateria == null){
                    return NotFound();
        }
   }
}

to do a double query and get a list, but I need to receive two parameters in the controller so I can do that search

    
asked by José MN 03.11.2016 в 17:40
source

1 answer

1

With the little information in the question, here's an answer:

Assuming that:

  • The following JavaScript will be called from a page with Razor (.cshtml):

  • There is a Div where the result of the shipment will be placed.

  • We have the following code:

    $.ajax({
     type: "POST",
     url: '@Url.Action("GetComplementoMateriaId","NombreDeTuControlador")',
     data: {id:ValorDelID, IdUnidad:ValorDelIdUnidad},
     contentType: "application/json; charset=utf-8",     
     success: function (response) {
          $('#resultado').html('');
          $('#resultado').html(response);
    });
    
        
    answered by 03.11.2016 в 18:00