2012-04-01 6 views
1

Я пытаюсь передать параметр, который является «priceValue». Как передать это значение с помощью RedirectToAction? или у вас есть идея для этого? Я пытаюсь сделать корзину покупок. «priceValue» - значение radioButton. Не могли бы вы мне помочь? После передачи priceValue, и я хочу использовать if statement, что я написал в 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

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

перенаправит его к методу действий под названием Index с параметром ид

Смежные вопросы