0

I am using ASP.net MVC with the razor engine.

I have a page with a button, when the button is clicked i call an ActionResult in my controller using Ajax. The Ajax call:

 $.ajax({
     type: "GET",
     url: "/Home/Review",
     success: function (data) {
         //alert("yay");
     }
});

My ActionResult method is hit fine, however it does not load the view speified. The code:

public ActionResult Review()
{
    return View();
}

The current view is Index.cshtml when Review() is hit i would like Review.cshtml to be rendered on screen. (It should be rendered as a full view, not as partial view inside Index.cshtml)

I have tried - return View("Review"); return View("Review.cshtml"); return View("..\Views\Home\Review.cshtml);

I can't use - tml.BeginForm("Action", "Controller", FormMethod.Post); (see my other question MVC submit button not firing)

Community
  • 1
  • 1
DNKROZ
  • 2,334
  • 3
  • 22
  • 41
  • The `data` parameter of the `success` callback function, should contain the response from the server. – haim770 Jul 09 '14 at 12:45
  • You want to include the content of Review.cshtml inside Index ? If not why do you do an ajax call ? – NicoD Jul 09 '14 at 12:47
  • @NicoD No, i want to load Review instead of index.. like navigating to a different page – DNKROZ Jul 09 '14 at 12:48
  • 1
    Just submit the form to a specific action Html.BeginForm("Action", "Controller", FormMethod.Post); or use a link in this case. No need for ajax. – NicoD Jul 09 '14 at 12:52
  • Just edited post to show why i cant use that. Also i will be sending some data later so will need ajax anyway – DNKROZ Jul 09 '14 at 13:02

2 Answers2

2

Write the response directly to the screen

success: function (data) {
   document.write(data)  }
Arjuna
  • 324
  • 1
  • 17
  • 3
    Maybe you should read : [Why is document.write considered a “bad practice”?](http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice) – NicoD Jul 09 '14 at 13:02
0

After ajax, you should render a partial view to add returning result to main view(index).

Index.cshtml

$.ajax({
   type: "GET",
   url: "/Home/Review",
   success: function (data) {
       $("#container").html(data);
       //alert("yay");
   }
...

<div id="container"></div>

Controller

public ActionResult Review()
{

    return PartialView();
}

If you want to redirect after ajax you can use something like following:

Index.cshtml

$.ajax({
   type: "GET",
   url: "/Home/Review",
   success: function (data) {
       window.location.href = data.url;
   }
...

Controller

public ActionResult Review()
{

    return Json(new { url = "some url" }, JsonRequestBehavior.AllowGet); 
}
AliRıza Adıyahşi
  • 14,706
  • 23
  • 108
  • 189