0

I cannot get another view to load after initial load of my MVC project. I am using an ajax call to run a method called SlideLayoutView within a controller called SlideView.

Here is the ajax call:

     $.ajax({
                url: "/SlideView/SlideLayoutView",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(id),
                success: function (result) {

                },
            });

Here is the route for it:

        //slide layout view
        routes.MapRoute("SlideViewPage",//Route name
                        "SlideView/SlideLayoutView/{id}",//URL
                        new { controller = "SlideView", action = "SlideLayoutView", id = UrlParameter.Optional },// paramter defaults
                        new[] { "ChootaAuthor.web.Controllers" });

And here is the method within the SlideViewController:

        public ActionResult SlideLayoutView(int id = 0)
    {
        ViewBag.Message = "Slide View Page";

        var model = new SlideViewModel();

        model.slideID = 1;
        model.slideTitle = "test";

        return View("SlideLayoutView", model);
    }

If I run the view itself it works fine. If I navigate to it with /SlideView/SlideLayoutView/-1 it works fine. But shouldn't it work as well if I return the View? Please help

James Blackburn
  • 594
  • 4
  • 23
  • 1
    I don't really understand the problem you're having. Your code seams to be working but the part where you're injecting the view inside your DOM is missing. Is that all there is to your AJAX `success` function? How do you insert your `result`? – Andrei V Feb 04 '15 at 08:44
  • It should be `data: { id: 1 },` (assuming you want to pass `1` to the controller) –  Feb 04 '15 at 08:52
  • The id gets passed through fine and the return View is hit. I thought return View would show the view, is there anything extra I need to do from that point? – James Blackburn Feb 04 '15 at 10:09
  • For temporary solution I have hardcoded the url but dont want it to stay like that – James Blackburn Feb 04 '15 at 10:30
  • The value of `result` in your ajax success callback is the view you returned. What do you want to do with it? –  Feb 04 '15 at 10:59
  • I want to display the returned view, so I want the returned view to replace the current one – James Blackburn Feb 04 '15 at 12:53
  • Now solved. I thought returning the view from the controller would somehow load the view but now I realise how stupid me thinking that was. I replace the page wrapper div contents with the html contained in the result in the success method of the ajax call – James Blackburn Feb 04 '15 at 15:48

1 Answers1

1

You aren't doing anything with the AJAX result after calling the view.

 success: function (result) {
NOTHING HAPPENS HERE, SO YOU CANT EXPECT ANYTHING TO HAPPEN
                },
user3036342
  • 1,023
  • 7
  • 15
  • I didnt think I would have to do anything at that point I thought the return View in the controller method would load it. How can i replace the current view then with the one being returned? – James Blackburn Feb 04 '15 at 12:56
  • You are using AJAX to execute and return the View. That works 100%. However you're not doing anything with it. Client-side code cannot interact with Server-side code the way you think (or assume) it would work. Don't use AJAX in this case and just do a normal post to the view. If you don't want the page to reload, use AJAX, but then actually do something with the AJAX result. In Chrome, you can use the XHR portion of their dev tools to see the result coming back from the server when using AJAX. You would need to update the DOM with the result to see the result – user3036342 Feb 04 '15 at 12:59
  • I get ya. I was under the impression that returning the view from my controller method would somehow load the new view. I'm new to mvc and thought that was how it might work. I am now replacing the page wrapper div contents with the result in my onsuccess message. Thanks for the help – James Blackburn Feb 04 '15 at 15:47
  • No worries. With MVC doing so much for us in the background these days, it doesn't surprise me that you made the assumption :D – user3036342 Feb 05 '15 at 06:35