控制器重定向不执行方法

时间:2018-06-15 12:33:53

标签: c# asp.net-mvc

遵循所有建议的补救措施,我的方法仍未被调用。

被调用的方法在同一个控制器中。 TEST可以通过单独页面上的链接自动调用,也可以在用户选择希望再次加载的已保存“TEST”视图时调用。在这种情况下,调用下面的LoadUserSelectedTransaction

我已尝试指定控制器使用但没有运气,硬编码requestType,[HTTPPOST]属性,从视图中删除FormMethod.Post

查看如下:

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

控制器如下

    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);
            }
            }

被调用的方法:

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);
        }

非常感谢任何建议,谢谢!

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

1 个答案:

答案 0 :(得分:0)

RedirectToAction会向浏览器返回302重定向,这始终是GET请求。

比较Server.TransferRequest(url, true)执行服务器重定向,使用POST请求保留postBack值。

P.S。浏览器不会知道此重定向,因此浏览器网址不会反映新地址