0

I am new in MVC .net. I am not able to call view from controller. I have debug the flow. It goes to view successfully but doesn't show view on screen.

Controller name: Downloads
Action name: MakePayment
Redirect view: Success    //success is view of View/Downloads/Success.cshtml

Code: DownloadsController

[HttpPost]
public ActionResult MakePayment(Downloads CCM)
{
     if (true)
     {
          return View("Success");
     }
     else
     {
          return View("Failure");
     }
}

View

@{
    ViewBag.Title = "Success";
}

<h2>Your transaction has been completed successfully.</h2>

Method that I use to call the ActionResult MakePayment. I had use Ajax here because I wanted to call javascript function before form get submit.

View: Index.cshtml

@using (Ajax.BeginForm("MakePayment", "Downloads", new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, OnBegin = "payByCreditCard" }))
{
   //submit button
}

According to the return View("Success");, it should call the view. In fact when I debug the flow, it goes to Success view but doesn't display the view on screen. It keeps old view on screen.

Degug route after success: _ViewStart-->Success.cshtml-->_Layout.cshtml.

Can anybody suggest me if I am missing something?

Nanji Mange
  • 2,155
  • 4
  • 29
  • 63
  • have you an `ActionResult` for the Success view? – user1666620 Jul 28 '16 at 14:26
  • What is your old view ? Can you show your complete (relevant) code which returns both the views ? Also do you have this `if(true)` hardcoded ? Then it should return the Success view. – Shyju Jul 28 '16 at 14:26
  • @user1666620 yes. it make payment using creditcard. I can't post whole code. I just want to redirect to success page when payment get success. – Nanji Mange Jul 28 '16 at 14:30
  • @Shyju Old View was `Index`. – Nanji Mange Jul 28 '16 at 14:32
  • Your code should work fine. But i suggest you follow the PRG pattern http://stackoverflow.com/questions/10628476/mvc3-prg-pattern-with-search-filters-on-action-method – Shyju Jul 28 '16 at 14:33
  • @Shyju I have added view code from where I call ActionResult MakePayment. It has Ajax call to submit. – Nanji Mange Jul 28 '16 at 14:38

2 Answers2

2

Since you are making an ajax call using Ajax.BeginForm helper method, you need to specify where the response (of the ajax call) to be replaced in the DOM. You can specify that using the UpdateTargetId property

@using (Ajax.BeginForm("MakePayment", "Downloads", new AjaxOptions { HttpMethod = "POST", 
                                      InsertionMode = InsertionMode.Replace,
                                      UpdateTargetId="YourDivToShowResult"
                                      OnBegin = "payByCreditCard" }))
{
  <div id="YourDivToShowResult"></div>
  <input type="submit" />
}

Since this is an ajax call, It will not do a redirect. Instead,it will update the content of the div (with Id YourDivToShowResult) in the same page with response coming back which is the markup returned by Success view.

Also since we are showing a partial page update, you might consider returning a partial view.

[HttpPost]
public ActionResult MakePayment(Downloads CCM)
{
     if (everything is good)
     {
          return PartialView("Success");
     }
     else
     {
          return PartialView("Failure");
     }
}

This will return the markup from either of those views without the layout.

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Thank you. But I don't know how to return from MakePayment ActionResult. You said to show div using `UpdateTargetId`. If the payment fail, I want to show failure result. If payment get success, I want to show success message. Can you please suggest me code for `public ActionResult MakePayment(Downloads CCM)` action? – Nanji Mange Jul 29 '16 at 04:37
  • You already have code for returning either the success or failure view. put whatever message you want to show in those view files. – Shyju Jul 29 '16 at 13:04
-1

If you want to go to other view you have to redirect the page like:

return RedirectToAction("Success", "Downloads");
Corina Gheorghe
  • 198
  • 1
  • 9
  • This is not correct information. You can still return a different view from HttpPost action method ( But redirecting is an ideal solution as it follows PRG pattern) – Shyju Jul 28 '16 at 14:30
  • I have added view code from where I call ActionResult MakePayment. – Nanji Mange Jul 28 '16 at 14:38
  • I have also tried by your code. It redirect to Success but doesn't show the Success page on current screen even it route to _ViewStart-->Success.cshtml-->_Layout.cshtml. – Nanji Mange Jul 28 '16 at 14:42
  • Now because you said that you use ajax, today i just have this kind of problem and my aproach was to give up to use ajax, i found an work around – Corina Gheorghe Jul 28 '16 at 14:46