0

I have the following Ajax Call:

$.ajax({
  url: '@Url.Action("DeleteUser", "Tribes")',
  dataType: "json",
  type: "Post",
  data: {
    "tribeId": tribeId,
    "buId": buId,
    "userId": userId
  },
  success: function(data) {
    if (data.status === "success") {
      $.smallBox({
        title: "<strong>User Deleted</strong>",
        content: "<i class='fa fa-clock-o'></i> <i>Tribe user was successfully deleted! <strong></strong></i>",
        color: "#659265",
        iconSmall: "fa fa-check fa-2x fadeInRight animated",
        timeout: 4000
      },
      function(data) {
        window.location.href = '@Url.Action("ViewTribeUsers", "Tribes", new 
        { 
          tribeId = data.tribeId, 
          buId = data.buId 
        })';
      });
    }
  }
});

My controller looks like this:

public ActionResult DeleteUser(int tribeId, int buId, string userId)
{
  clsTribes.DeleteTribeUsers(tribeId, userId);
  return Json(new 
  { 
    status = "success", 
    tribeId = tribeId, 
    buId = buId 
  }, JsonRequestBehavior.AllowGet);
}

How do I get to use the data.[variables] after the AJAX call? In this instance, the error I am getting is on the data.[variable] in this line:

 window.location.href = '@Url.Action("ViewTribeUsers", "Tribes", new 
 { 
   tribeId = data.tribeId, 
   buId = data.buId 
 })';

Error: The name 'data' does not exist in the current context

I am sure it must be simple, any help would be much appreciated!

Thanks!

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
AxleWack
  • 1,801
  • 1
  • 19
  • 51
  • I assume you mean in the function(data) part ? I originally didnt have it there and still getting the same issue... – AxleWack Sep 19 '18 at 15:14
  • `data` is a JS variable provided from the callback of `$.smallBox` yet you are attempting to use it in the C# server side code. That is not possible to achieve in the manner you're attempting. – Rory McCrossan Sep 19 '18 at 15:15
  • (Thanks for the edit on spacing) Can you provide in an answer how I should do it ? I need to pass the variables from Data to the inner function, to be able to refresh my page. – AxleWack Sep 19 '18 at 15:19
  • Sure, I added an answer for you – Rory McCrossan Sep 19 '18 at 15:26

1 Answers1

0

The issue is because data is a client-side JS variable, and you cannot use that in your server-side C# code.

One possible workaround would be to use Url.Action to build the base URL then append the properties from data to it, something like this:

function (data) {
  window.location.href = '@Url.Action("ViewTribeUsers", "Tribes")?tribeId=' + data.tribeId + '&buId=' + data.buId;
});

The downside of this is that you won't have the benefit of routing for this URL. It also becomes harder to maintain as you need to remember to update this JS logic if you change your routing pattern.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • Thanks! That took away the error, but now im getting an error in my console(F12) : Uncaught ReferenceError: deleteTribeUser is not defined – AxleWack Sep 19 '18 at 15:41
  • That appears to be a similar issue. `deleteTribeUser` is a method on `clsTribes` in your C# code, yet if the error is appearing in the console that means you're calling it from the client side. It seems as though you're a little confused about the difference between client and server side code. I'd suggest doing [some research](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) on the difference, and then ask a new question about that issue if you still have problems. – Rory McCrossan Sep 19 '18 at 15:45
  • The naming of the function in class clsTribes and the function I am trying to call from the onclick for the ajax call was pure coinicidence. I have changed the function name, but still getting the same error. Thanks for the help nevertheless. Your answer gave me what I asked for technically. So will mark as the answer. – AxleWack Sep 19 '18 at 15:48
  • So just to confirm - It seems the problem I was facing was because the delete function needed to be OUTSIDE of my $(document).ready(function () { ... } Strange....I will have to read up on this and find out why... – AxleWack Sep 19 '18 at 15:59