1

Here is the relevant View cshtml code

<tbody>
    @foreach (var task in Model.TasksSummaryList)
    {
        <tr>
            <th onclick="OnPatientSelected('@task.PatientID')">@task.PatientName</th>
            <th>@task.Status</th>
            <th>@task.TaskCount</th>
            <th>@task.MostRecentTask</th>
        </tr>
    }
</tbody>

Here is the external javascript file containing OnPatientSelected

function OnPatientSelected(selectedPatientID) {
    $.ajax({
        type: "POST",
        url: "TasksSummary/TaksSummaryDetailsView",
        data: { selectedPatientID: selectedPatientID },
        error: function () {
            alert("fail");
        },
        success: function(){
        alert("success");
        }
    });
    }

This always shows success but never calls the Controller ActionMethod I want

[AllowAnonymous]
        [HttpPost]
        public ActionResult TasksSummaryDetailsView(/*data from view*/string selectedPatientID)
        {
            int i = 09;
            //Received PatientID from View (Client)
            //Browse to the appropriate view 
            return View();
        }
Ankit S
  • 9
  • 3
  • can you keep a breakpoint in your action method and check if its being hit – Karthik M R May 29 '16 at 17:16
  • How have you verified that the action isn't being invoked? Also, note that the `url` provided is [relative to the current page's address](https://stackoverflow.com/questions/4765740/relative-urls-in-ajax-requests) and may not result in the path you expect. – Jonathan Lonowski May 29 '16 at 17:41
  • Try using fiddler to see what calls are being made and whats the output. Also, try the url directly in browser to see if it works : `.../TasksSummary/TaksSummaryDetailsView?selectedPatientID="yourvalue"` – Polynomial Proton May 29 '16 at 17:43
  • So the only way it works in browser is if I remove the [HttPost] tag on top of method name but when I place it, I get an error saying ActionName does not exist....... A public action method 'TasksSummaryDetailsView' was not found on controller 'eRxWeb.Areas.CodeAThonMVC.Controllers.TasksSummaryController'. – Ankit S May 29 '16 at 18:52

2 Answers2

1

The path needs to be relative, try something like the following:

url: "../TasksSummary/TaksSummaryDetailsView",

Or even better:

url: '@Url.Action("TasksSummary", "TasksSummaryDetailsView")',
James P
  • 2,201
  • 2
  • 20
  • 30
  • So I tried all possible combinations. Still not working. My controller file is TasksSummaryController.cs and that contains HttpPost tagged action method TasksSummaryDetaillsView – Ankit S May 29 '16 at 18:27
-1

So replaced the url line in ajax call with the following and it works

url: 'TasksSummaryDetailsView'

Ankit S
  • 9
  • 3
  • You didn't explain why this works. Do you understand why this fixed the problem? Or did you just repeat what James P already said in his answer? – Robert Harvey May 30 '16 at 00:02
  • So since the paths were relative, using the above line worked. No F12 network errors. – Ankit S Jun 01 '16 at 15:30