0

Following all suggested remedies, still my method is not getting called.

The method being called is in the same controller. TEST can be called either by itself through a link on a separate page, or when a user selects a saved "TEST" view that they wish to load again. In this case, the below LoadUserSelectedTransaction is called.

I've tried specifying the controller to use with no luck, hard coding requestType, [HTTPPOST] attribute, removing FormMethod.Post from the view

View is as follows:

using (Html.BeginForm("LoadUserSelectedTransaction", "TransactionList", FormMethod.Post))

Controller is as follows

    public class TransactionListController : Controller
            {
            public static bool userLoaded = false;
            public static string userFriendlyName;
            public static string transaction;
            public static string requestType;


            //[HttpPost]
            public ActionResult LoadUserSelectedTransaction(FormCollection formdata)
            {
                userLoaded = true;
                userFriendlyName = formdata["UserLoadedTransaction"];

                var model = new MyTransactionsDBHandle();
                transaction = model.ReturnUserLoadedTransaction(userFriendlyName, User.Identity.Name);

                var myTransaction = new MyTransactionsModel();
                requestType = myTransaction.ReturnRequestType(transaction);

                if (requestType == "")
                {
                    requestType = formdata["CurrentTransactionPage"];
                }
                // need to check for duplicate transactions types, such as CC_SALE also being a POS Surcharge transaction
                // CC_SALE has EMV, ship to, bill to etc, POS Surcharge does not
                // CC_VOID with and without card - With card will have NGT_TRACKDATA tag
                // DB_SALE with and without cashback - Cashback will have NGT_DC_CASH_BACK_AMOUNT tag
                if (requestType == "DB_SALE")
                {
                    if (transaction.Contains("NGT_DC_CASH_BACK_AMOUNT"))
                        return RedirectToAction("DB_Sale_With_Cash_Back");
                    return RedirectToAction("DB_Sale_No_Cash_Back");
                }
                else if (requestType == "CC_SALE")
                {
                    if (transaction.Contains("NGT_EMV_DATA"))
                        return RedirectToAction(requestType);
                    return RedirectToAction("CC_POS_Surcharge");
                }
                else if (requestType == "CC_VOID")
                {
                    if (transaction.Contains("NGT_TRACKDATA"))
                        return RedirectToAction(requestType);
                    return RedirectToAction("CC_Void_No_Card");
                }
                else
                    return RedirectToAction(requestType);
            }
            }

Method being called:

public ActionResult TEST(FormCollection formdata)
        {
            ViewModel model = new ViewModel();
            if (userLoaded) // static in same controller
            {
                userLoaded = false;
                if (requestType == "My Transactions")
                {
                    var currentTransactionPage = formdata["CurrentTransactionPage"];
                    return RedirectToAction(currentTransactionPage);
                }
                model.myTransactions.ParseUserTransactionString(transaction, model);
            }
            return View(model);
        }

Any suggestions are greatly appreciated, thanks in advance!

RouteConfig.cs

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "User", action = "Login", id = UrlParameter.Optional }
        );

        routes.MapRoute(
           name: "ClientTablePages",
           url: "{controller}/{action}/{pageNumber}",
           defaults: new { controller = "Client", action = "Index", page = UrlParameter.Optional }
        );

        routes.MapRoute(
          name: "DivisionTablePages",
          url: "{controller}/{action}/{pageNumber}",
          defaults: new { controller = "Division", action = "Index", page = UrlParameter.Optional }
       );

        routes.MapRoute(
          name: "MerchantTablePages",
          url: "{controller}/{action}/{pageNumber}",
          defaults: new { controller = "Merchant", action = "Index", page = UrlParameter.Optional }
       );

        routes.MapRoute(
          name: "TerminalTablePages",
          url: "{controller}/{action}/{pageNumber}",
          defaults: new { controller = "Terminal", action = "Index", page = UrlParameter.Optional }
       );

        routes.MapRoute(
            name: "ResetPassword",
            url: "{controller}/{action}/{id}",
            defaults: new { Controller = "User", action = "UserResetPassword", id = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "MyTransactions",
            url: "{controller}/{action}/{pageNumber}",
            defaults: new { Controller = "MyTransactions", action = "MyProfile", page = UrlParameter.Optional }
        );
    }
}

302 Response

Value of requestType

TEST method added

Lines above TEST

Jason Beck
  • 23
  • 7
  • The exact value of requestType is a variable string. Depending on whether or not the user wants to view a 'TEST' transaction or a 'CC_SALE' transaction – Jason Beck Jun 15 '18 at 12:51
  • TEST is in TransactionListController, yes. ViewModel model = new ViewModel(); is not being hit. The redirecttoaction is not redirecting to that method which is the issue. – Jason Beck Jun 15 '18 at 12:56
  • https://localhost:44374/TransactionList/LoadUserSelectedTransaction – Jason Beck Jun 15 '18 at 12:58
  • When you post to that endpoint it would have returned a 302 to another URL. What is that URL? – mjwills Jun 15 '18 at 13:05
  • TransactionListController updated to show it's entirety – Jason Beck Jun 15 '18 at 13:13
  • I think its better to refactor to make a function calls inside if branches, instead of redirecting. – valerysntx Jun 15 '18 at 13:18
  • If I refactor to function calls, users won't be able to select directly from the list of available transaction type choices without writing out again all the return View(model); 's. – Jason Beck Jun 15 '18 at 13:24
  • POST is commented out, I had it as an attribute in the LoadUserSelectedTransaction ActionResult method. My view however, has FormMethod.POST. TEST has no attributes. Updated post to include picture of it. – Jason Beck Jun 15 '18 at 13:28
  • Seems like this is a tricky thing to do. You can use ajax requests as suggested in this post https://stackoverflow.com/questions/46582/response-redirect-with-post-instead-of-get – Aman B Jun 15 '18 at 13:40
  • The lines above are more of the same as TEST, but just different transactions types. I edited my post with a picture of this – Jason Beck Jun 15 '18 at 13:46
  • Have you tried `Server.TransferRequest(url, true)` ? – Aman B Jun 15 '18 at 14:00
  • I have not tried Server.TransferRequest before, but after giving it a go, it may do the trick. Just need to alter my LoadUserSelectedTransaction ActionResult to void – Jason Beck Jun 15 '18 at 14:17
  • 1
    @AmanB , did you want to make a post so that I can accept as answer? – Jason Beck Jun 15 '18 at 17:53

1 Answers1

0

RedirectToAction returns a 302 redirect to the browser which is always a GET request.

In comparison Server.TransferRequest(url, true) performs a server redirect preserving the postBack values using a POST request.

P.S. Browser won't be aware of this redirect hence the browser url will not reflect the new address

Aman B
  • 2,276
  • 1
  • 18
  • 26