0

Ok so the situation is that I am on my index page. I have a jQuery onClick event as follows:

$('#example').on('click', 'tr', function (e) {
    var aData = oTable.fnGetData(this);
    showCampaign(aData);
});

I have verified that this function is working as intended and aData winds up populated with an array containing the data from each cell in the table row that gets clicked.

What i need to do is when this click even occurs shoot aData to my MVC and load a new view that i can populate with that data. This is what im currently trying....

function showCampaign(aData) {
    $.ajax({
        type: "POST" ,
        url: "Test/Show",
        data : aData
   }).done(function (response) {
        window.location.href = response.Url;
   }).error(function (jqxhr, textstatus, errorthrown) {
        alert("didn't work");
   });
};

This function works to a point ... it does send a request to my MVC ... but I am not seeing the data come through (its null) and when I return the response string the page does not load.

The method in my Controller class looks like this :

[HttpPost]
public ActionResult ShowCampaign(String[] aData)
{
   var redirectUrl = new UrlHelper(Request.RequestContext).Action("TestView","Test");
   return Json(new {Url : redirectUrl });
}

For purposes of the above TestView is the name of the View i want to load and Test is the Controller.

What am I doing wrong here.. Im trying to understand MVC but I just dont get it yet ... the light hasnt clicked on.

Deslyxia
  • 619
  • 4
  • 11
  • 32

1 Answers1

1

The problem here is that you are not naming the parameter. If like you say aData is an array you have to wrap it into an object. The property should match the controller method's parameter name.

function showCampaign(aData) {
$.ajax({
    type: "POST" ,
    url: "Test/ShowCampaign",
    data: { aData: aData }
}).done(function (response) {
    window.location.href = response.Url;
}).error(function (jqxhr, textstatus, errorthrown) {
    alert("didn't work");
   });
};

Another tip you might find useful. You can make use of Url.Action() to generate your URLs. http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.action(v=vs.108).aspx

Dan
  • 1,878
  • 13
  • 17
  • I encapsulated the data in an object as you suggested and it works fine. At this point i am getting a url response back and it tries to redirect as i would expect ..... but it says that the resource cannot be found the url being given is localhost:"port"/Test/TestView I have verified that under the Solution Explorer that my index.aspx and TestView.aspx are both located under the Test folder within Views... What am i missing – Deslyxia Oct 02 '13 at 23:03
  • Do you have a controller named TestController in your project? – Dan Oct 02 '13 at 23:05
  • The controller is named TestController and thats where the ShowCampaign() function is located. – Deslyxia Oct 02 '13 at 23:09
  • And does that controller also have a method called TestView? That's where the call to localhost/Test/TestView should take you. In there should be the `return View();` line. – Dan Oct 02 '13 at 23:14
  • that works... thank you ... one last tiny question ... I send the encapsulated array to the HttpPost function in the controller ... which then returns the redirect url as Json.... how do i then populate the data all the way through to the actual landing page? – Deslyxia Oct 02 '13 at 23:20
  • If I understand correctly what you are trying to do then you can skip the redirecting. Couldn't you simply call `return new View(TestView);` in the ShowCampaign method? – Dan Oct 02 '13 at 23:23
  • but doesnt the return have to be json since it was returned to an ajax call? – Deslyxia Oct 02 '13 at 23:23
  • This might be what you're looking for then: http://stackoverflow.com/questions/1257482/redirecttoaction-with-parameter – Dan Oct 02 '13 at 23:38
  • http://stackoverflow.com/questions/6821597/how-to-redirect-a-post-request-to-a-url-maintaining-model-values-in-mvc seems like an option, too. – Dan Oct 02 '13 at 23:42
  • The method you linked too winds up linking to the action that would show athe other view ... but it returns the view as the response object to the original ajax call and doesnt redirect to that url – Deslyxia Oct 02 '13 at 23:55
  • Yes, but you can use the concept behind those articles to fix your problem. For example you could try using the RedirectToRoute method of the controller and give your parameter in the routeDictionary. Or you could follow the advice in the second article I linked and cache the parameter values in one method and read from the cache in the method you redirected to. – Dan Oct 03 '13 at 09:30