2012-04-01 22 views
1

我试图传递参数,即'priceValue'。我怎样才能通过使用RedirectToAction传递这个值?或者你有什么想法吗? 我正试着让购物车现在。 'priceValue'是radioButton的值。你能给我一些帮助吗?在传递了priceValue之后,我想用if语句写我在AddToCart中写的东西。可以使用它吗? 请帮我.. 谢谢。MVC3将参数传递给同一控制器中的其他人

public class ShoppingCartController : Controller 
{ 
rentalDB db = new rentalDB(); 
    // 
    // GET: /ShoppingCart/ 
    public ActionResult Index() 
    { 
     var cart = ShoppingCart.GetCart(this.HttpContext); 

     // Set up our ViewModel 

     var viewModel = new ShoppingCartViewModel 
     { 
      CartItems = cart.GetCartItems(), 
      CartTotal = cart.GetTotal() 
     }; 

     // Return the view 
     return View(viewModel); 
    } 


    // GET: /Store/AddToCart/5 
    [HttpPost] 
    public ActionResult AddToCart(int id, FormCollection col) 
    { 
     var addedProduct = db.Product 
      .Single(product => product.productId == id); 
     decimal priceValue = Convert.ToDecimal(col["price"]); 
     //how to pass priceValue to index 
     if (Convert.ToDecimal(col["price"]) == addedProduct.threeDayPrice) 
     { 
      ViewBag.price = new SelectList(db.Product, "productId", "threeDayPrice"); 
      //How to put this value into cart.AddtoCart(addedProduct) with date and price 
     } 
     else if (Convert.ToDecimal(col["price"]) == addedProduct.aWeekPrice) 
     { 
      ViewBag.price = new SelectList(db.Product, "productId", "aWeekPrice"); 
      //How to put this value into cart.AddtoCart(addedProduct) with date and price 
     } 

     // Retrieve the product from the database 
     // Add it to the shopping cart 
     var cart = ShoppingCart.GetCart(this.HttpContext); 

     cart.AddToCart(addedProduct); 

     // Go back to the main store page for more shopping 
     //I don't know how to pass the 'priceValue' by using RedirectToAction. 
     return RedirectToAction("Index", new {id = priceValue}); 
    } 

回答

2

你有一个参数,以便添加到您的索引方法能够获得在传递到价值索引方法:

public ActionResult Index(int priceValue = 0). 

而不是0,然后可以使用任何您希望的默认值。然后致电

return RedirectToAction("Index", new {@priceValue = priceValue}); 

将允许您获取方法内的值。

+0

谢谢你的回复!我已经知道了。谢谢! – wholee1 2012-04-02 03:52:41

1
return RedirectToAction("Index", new {@id = priceValue.toString()}); 

将其重定向到被称为指数与id参数的操作方法

相关问题