2016-12-26 58 views
0

我嘗試編寫一個方法,當它將用戶重定向到另一個控制器內的動作時。我使用return Redirect("~/" + redirectUrl + "/" + paymentInfo.ObjectIdDepartue);陳述來做到這一點。並通過RedirectResult,ActionResult測試此方法的類型,但不工作。如何將用戶重定向到另一個控制器內部的動作

什麼是問題?

public RedirectResult PayViaCredit(int paymentId) 
     { 
      var paymentInfo = db.Payments.Find(paymentId); 
      //Decrease Credit 
      Profile userProfile = _utility.GetProfileOfUser(); 
      decimal decreasedCredit = userProfile.Credit - paymentInfo.Price    using (var transactionScope = new TransactionScope()) 
      { 
       try 
       { 
        db.ProcUpdateProfileCredit(decreasedCredit, userProfile.UserId); 
        //Update Payment 
        paymentInfo.Transactionsuccess = true; 
        paymentInfo.state = 3; 
        paymentInfo.Confirmed = true; 
        db.Entry(paymentInfo).State = EntityState.Modified; 
        db.SaveChanges(); 
        System.Web.HttpContext.Current.Session["InvoiceNumber"] = paymentInfo.Id.ToString(); 
        transactionScope.Complete(); 
       } 
       catch (Exception e) 
       { 
        _errorLog.Error("CreditPayment.cs/PayViaCredit", "", e.Source, "Message:::" + e.Message + "---InnerException:::" + e.InnerException); 
        transactionScope.Dispose(); 
       } 

      } 

      string redirectUrl = _creditPayment.GetRedirectUrlCreditPayment(paymentInfo); 
      return Redirect("~/" + redirectUrl + "/" + paymentInfo.ObjectIdDepartue); 
     } 
public ActionResult AfterPaymentBank() 
    { 
    return View(); 
    } 
+0

'redirectUrl'是否包含絕對Url?無論如何,沒有理由使用'〜'來進行重定向。 – haim770

+0

我的網址是〜/ Charter/AfterPaymentBank。我測試出來〜但不起作用 – programmer138200

+0

你可以發佈AfterPaymnetBank行動的代碼嗎? – FakeisMe

回答

0

首先將您的數據保存在TempData中。

TempData["myData"] = paymentInfo.ObjectIdDepartue; 

然後

return Redirect("/ControllerName/ActionName"); 

永遠是你的的redirectUrl,給它這樣的形狀。

TempData["myData"] = paymentInfo.ObjectIdDepartue; 
redirectUrl = redirectUrl.Substring(1); // or what ever you thing appropriate for your URL 
return Redirect(redirectUrl); 

現在在「ActionName」操作方法中,將您的TempData類型轉換爲您需要的類型。

public ActionResult ActionName() 
{ 
var myData=(myType)TempData["MyData"]; 
// and proceed.... 
} 
相關問題