2

I'm trying to call a MVC Controller action with AJAX passing some parameters.
I have done this sometimes in this application and it works fine.
I have no idea why only THIS ONE doesn't work.

 function excluirDetalhe(button, tab) {
     var index = $(button).closest('tr').index();
     var myTable = document.getElementById(tab);
     var id = myTable.rows[index].cells[0].innerHTML.trim();
     $('#' + tab + ' tr:eq(' + index + ')').remove();
         $.ajax({                
             traditional: true,
             url: "entidades/gravaCookie",
             type: 'post',
             data: { id: id, detalhe: "E" },
             error: function (jqXHR, textStatus, errorThrown) {
                 alert(errorThrown);
             }
     });
 }

This is my controller method:

public void gravaCookie(string id, string detalhe)
{
     string key = "een_id";
     if (detalhe.Equals("E"))
         key = "een_id";
     if (detalhe.Equals("C"))
         key = "eco_id";
     if (detalhe.Equals("B"))
         key = "eba_id";
     cookie.Values.Add(key, id);
}

Just a reminder that I'm doing exactly I did in other Ajax calls, but only this one in particular is not working.
Does anyone have any idea?

cybertextron
  • 10,547
  • 28
  • 104
  • 208
henrique romao
  • 560
  • 1
  • 8
  • 32
  • 1
    Do you have any script errors ? Are you seeing a 404 error message ? – Shyju Dec 17 '15 at 19:39
  • 3
    Do you have [HttpPost] above the controller method? – Nate Dec 17 '15 at 19:40
  • Along the lines of what @Nate said: if you have [HttpGet] on The gravacookie controller method then you'll get a 404. – jnoreiga Dec 17 '15 at 20:03
  • It doesn't look like your controller action is returning anything. First your your method is void, second it is not a MVC ActionResult or JsonResult. If you are doing post you must decorate your method with [HttpPost]. – Hasta Tamang Dec 17 '15 at 20:34

1 Answers1

8

Always use the Url.Action or Url.RouteUrl html helper methods to build the url to the action methods. It will take care of correctly building the url regardless of your current page/path.

Assuming your js code is inside a razor view, you can directly use the Url.Action method and assign that to your js variable.

url: "@Url.Action("gravaCookie","entidades")",

It should work assuming you have an action method like this

[HttpPost]
public ActionResult gravaCookie(string id,string detalhe)
{
  // to do : Return something
}

If your javascript is inside a seperate javascript file, you may build the url(s) in your razor view using the above helper methods and keep that in a variable which your external js file code can access. Always make sure to use javascript namespacing when doing so to avoid possible issues with global javascript variables.

@section Scripts
{
 <script>
    var myApp = myApp || {};
    myApp.Urls = myApp.Urls || {};
    myApp.Urls.baseUrl = '@Url.Content("~")';
    myApp.Urls.gravaCookieUrl = '@Url.Action("gravaCookie","entidades")';
 </script>
 <script src="~/Scripts/PageSpecificExternalJsFile.js"></script>

}

And in your PageSpecificExternalJsFile.js file, you can read it like

var urlToGrava= myApp.Urls.gravaCookieUrl
// Or With the base url, you may safely add the remaining url route.
var urlToGrava2= myApp.Urls.baseUrl+"entidades/gravaCookie";
// Use urlToGrava now

EDIT

If you simply care about the root/base url of the site, you may simply use / as the first character of your url.

var urlToGrava2= "/entidades/gravaCookie";
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • This does not pass his data back to the method. All Url.Action does is create the correct base url for the method, so he would also need to include the data. – Nate Dec 17 '15 at 19:44
  • 2
    @Nate That is not correct. He is passing his data in the `data` property and it will be properly mapped to the action method parameters. If you have doubt, Please feel free to paste the code and see. I am 100% positive. – Shyju Dec 17 '15 at 19:48
  • This also makes an assumption that his js is on the razor html page. – Nate Dec 17 '15 at 19:52
  • That is true. I updated the answer to address the other use case also. – Shyju Dec 17 '15 at 20:14
  • Thank's! Work like a charm – henrique romao Dec 18 '15 at 10:19