如何将用户重定向到另一个控制器

时间:2016-12-26 08:06:17

标签: asp.net-mvc redirect

我尝试编写一个方法,当调用它时,用户重定向到另一个控制器内的操作。我使用return Redirect("~/" + redirectUrl + "/" + paymentInfo.ObjectIdDepartue);语句来执行此操作。并且RedirectResultActionResult测试此方法的类型,但不起作用。

什么是问题?

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

1 个答案:

答案 0 :(得分:0)

首先将您的数据保存在TempData中。

return Redirect("/ControllerName/ActionName");

然后

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

你的 redirectUrl 是什么,给它一个像这样的形状。

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

现在在" ActionName"动作方法,键入你的TempData到你想要的类型。

 <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical">
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_marginTop="10dp"

            android:gravity="center"
            android:orientation="vertical"
            android:paddingBottom="-50dp" >

            <!---add your views here-->
        </LinearLayout>

        <ImageView
            android:id="@+id/imageView_close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|right"
            android:clickable="true"
            android:background="@drawable/crss" />
    </FrameLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp">

        <ImageView
            android:id="@+id/desimage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/logo" />

        <TextView
            android:id="@+id/h1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Chicken Pizza Small"
            android:textSize="21sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/h2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Chicken Pizza Small Combo"
            android:textColor="#8c8181"
            android:textSize="16sp" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="10dp"
            android:orientation="horizontal"
            android:weightSum="10">

            <Button
                android:id="@+id/quant"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginRight="10dp"
                android:layout_weight="3"
                android:background="@drawable/mybutton"
                android:text="Quantity"
                android:textAllCaps="false"
                android:textColor="#ffffff" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_weight="7"
                android:background="@drawable/stroke_button"
                android:orientation="horizontal"
                android:weightSum="3">

                <Button
                    android:id="@+id/incr"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@android:color/transparent"
                    android:text="-"
                    android:textColor="#FFA726"
                    android:textSize="25dp" />

                <View
                    android:layout_width="1dp"
                    android:layout_height="match_parent"
                    android:background="#c0c0c0" />

                <TextView
                    android:id="@+id/value"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:inputType="number"
                    android:text="1" />

                <View
                    android:layout_width="1dp"
                    android:layout_height="match_parent"
                    android:background="#c0c0c0" />

                <Button
                    android:id="@+id/decr"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@android:color/transparent"
                    android:text="+"
                    android:textColor="#FFA726"
                    android:textSize="25dp" />

            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp">

            <CheckBox
                android:id="@+id/sl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:buttonTint="#c0c0c0"
                android:text="SL"
                android:textColor="#8c8181"
                tools:targetApi="lollipop" />

            <CheckBox
                android:id="@+id/s"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:buttonTint="#c0c0c0"
                android:text="S"
                android:textColor="#8c8181"
                tools:targetApi="lollipop" />

            <CheckBox
                android:id="@+id/m"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:buttonTint="#c0c0c0"
                android:text="M"
                android:textColor="#8c8181"
                tools:targetApi="lollipop" />

            <CheckBox
                android:id="@+id/L"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:buttonTint="#c0c0c0"
                android:text="l"
                android:textColor="#8c8181"
                tools:targetApi="lollipop" />

            <CheckBox
                android:id="@+id/f"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:buttonTint="#c0c0c0"
                android:text="F"
                android:textColor="#8c8181"
                tools:targetApi="lollipop" />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:background="#c0c0c0" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="35dp"
                android:layout_marginLeft="7dp"
                android:layout_marginRight="15dp"
                android:layout_marginTop="5dp"
                android:layout_weight="1"
                android:background="@drawable/stroke_button">

                <Spinner
                    android:id="@+id/spin_1"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@android:color/transparent"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

                <ImageView
                    android:id="@+id/img_1"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_gravity="center_vertical"
                    android:layout_margin="5dp"

                    android:src="@drawable/spinpres" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="35dp"
                android:layout_marginLeft="7dp"
                android:layout_marginRight="15dp"
                android:layout_marginTop="5dp"
                android:layout_weight="1"
                android:background="@drawable/stroke_button">

                <Spinner
                    android:id="@+id/spin_2"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@android:color/transparent"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

                <ImageView
                    android:id="@+id/img_2"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_gravity="center_vertical"
                    android:layout_margin="5dp"

                    android:src="@drawable/spinpres" />
            </LinearLayout>
        </LinearLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="SPECIAL INSTRUCTIONS"
            android:textSize="16sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/stroke_button"
            android:padding="@dimen/fab_margin1">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/transparent"
                android:gravity="top"
                android:inputType="textCapSentences|textMultiLine"
                android:lines="5"
                android:maxLines="5"
                android:padding="2dp"
                android:textColor="#c0c0c0" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"

            android:layout_marginTop="5dp">

            <Button
                android:id="@+id/canc"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:background="@drawable/mybutton2"
                android:text="Cancel"
                android:textColor="#ffffff" />

            <Button
                android:id="@+id/ok"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_weight="1"
                android:background="@drawable/mybutton"
                android:text="Ok"
                android:textColor="#ffffff" />


        </LinearLayout>
    </LinearLayout>
</ScrollView>