2012-03-18 68 views
1

我新的MVC用戶,我試圖讓購物車如下MVC Music Store tutorialMVC3 ActionLink的與提交

我試圖通過單選按鈕值通過ActionLink的是不同的價格類型。 是否可以通過productId傳遞值? 當我點擊鏈接時,它會調用'AddToCart'方法。 你能幫我嗎?謝謝。

產品型號

namespace MvcApplication2.Models 
{ 
public class Product 
{ 
    [Key] public int productId { get; set; } 
    public int categoryId { get; set; } 
    [Required(ErrorMessage = "Product model name is required")] 
    public String model { get; set; } 
    [DisplayFormat(DataFormatString = "{0:0.#}")] 
    public decimal displaySize { get; set; } 
    public String processor { get; set; } 
    public int ramSize { get; set; } 
    public int capacity { get; set; } 
    public String colour { get; set; } 
    public String description { get; set; } 
    public decimal price { get; set; } 
    public decimal threeDayPrice { get; set; } 
    public decimal aWeekPrice { get; set; } 
    public decimal twoWeekPrice { get; set; } 
    public decimal aMonthPrice { get; set; } 
    public decimal threeMonthPrice { get; set; } 
    public decimal sixMonthPrice { get; set; } 
    //public decimal sixMonthPrice { get { return price * 0.25M; } } 
    public int stock { get; set; } 
    public virtual Category Category { get; set; } 
} 
} 

details.cshtml

@model MvcApplication2.Models.Product 
<td> 
     Rental Period: <br /> 
     @using (Html.BeginForm()) 
     { 
      <div class="display-label"> 
      @Html.RadioButtonFor(model => model.price, Model.threeDayPrice) 
      3 day: £@Model.threeDayPrice 
      </div> 
      <div class="display-label"> 
      @Html.RadioButtonFor(model => model.price, Model.aWeekPrice) 
      1 week: £@Model.aWeekPrice 
      </div> 
      <div class="display-label"> 
      @Html.RadioButtonFor(model => model.price, @Model.twoWeekPrice) 
      2 week: £@Model.twoWeekPrice 
      </div> 
      <div class="display-label"> 
      @Html.RadioButtonFor(model => model.price, @Model.twoWeekPrice) 
      1 month: £@Model.twoWeekPrice 
      </div> 
      <div class="display-label"> 
      @Html.RadioButtonFor(model => model.price, @Model.threeMonthPrice) 
      3 month: £@Model.threeMonthPrice 
      </div> 
      <div class="display-label"> 
      @Html.RadioButtonFor(model => model.price, @Model.sixMonthPrice) 
      6 month: £@Model.sixMonthPrice 
      </div> 
     }  
    </td> 
</tr> 
</table> 
<p class="button" style="margin-left:200px; width:90px;"> 
    //Is it possible to submit the selected radiobutton value through this? 
    @Html.ActionLink("Add to cart", "AddToCart", "ShoppingCart", new { id = Model.productId }, "") 
</p> 

---附加控制器--- ShoppingCartController.cs

public ActionResult AddToCart(int id) 
    { 
     // Retrieve the product from the database 
     var addedProduct = db.Product 
      .Single(product => product.productId == id); 

     // 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 
     return RedirectToAction("Index"); 
    } 

回答

2

只需使用一個,而不是提交的按鈕ActionLink的。這樣,當您提交表單中的所有輸入值將被髮送到控制器:

@model MvcApplication2.Models.Product 
<td> 
    Rental Period: <br /> 
    @using (Html.BeginForm("AddToCart", "ShoppingCart", new { id = Model.productId }, FormMethod.Post)) 
    { 
     <div class="display-label"> 
      @Html.RadioButtonFor(model => model.price, Model.threeDayPrice) 
      3 day: £@Model.threeDayPrice 
     </div> 
     <div class="display-label"> 
      @Html.RadioButtonFor(model => model.price, Model.aWeekPrice) 
      1 week: £@Model.aWeekPrice 
     </div> 
     <div class="display-label"> 
      @Html.RadioButtonFor(model => model.price, @Model.twoWeekPrice) 
      2 week: £@Model.twoWeekPrice 
     </div> 
     <div class="display-label"> 
      @Html.RadioButtonFor(model => model.price, @Model.twoWeekPrice) 
      1 month: £@Model.twoWeekPrice 
     </div> 
     <div class="display-label"> 
      @Html.RadioButtonFor(model => model.price, @Model.threeMonthPrice) 
      3 month: £@Model.threeMonthPrice 
     </div> 
     <div class="display-label"> 
      @Html.RadioButtonFor(model => model.price, @Model.sixMonthPrice) 
      6 month: £@Model.sixMonthPrice 
     </div> 

     <button type="submit">Add to cart</button> 
    }  
</td> 
+0

謝謝你的回覆。當我使用提交按鈕而不是actionlink時,shoppingCartController無法獲得int id。它不能通過productId。它發生錯誤,'參數字典包含一個null項爲參數'id'非空的類型'System.Int32'方法'System.Web.Mvc.ActionResult AddToCart(Int32)'在'MvcApplication2.Controllers.ShoppingCartController' ' – wholee1 2012-03-18 22:02:04

+0

@ wholee1,然後在生成表單時添加id:@using(Html.BeginForm(「AddToCart」,「ShoppingCart」,new {id = Model.productId},FormMethod.Post))''。我已經更新了我的答案,以舉例說明。我還刪除了不再需要的隱藏字段。 – 2012-03-18 22:05:00

+0

謝謝你現在正在傳遞productId。非常感謝! – wholee1 2012-03-18 22:07:53

0

因爲你說你是新的了蝙蝠的權利,我要告訴你,你是要對這個辦法並不是實現你想要實現的最好方式。

把一個在窗體底部提交按鈕將得到的數據發佈和綁定產品,如果你改變窗體頂部的

@using (Html.BeginForm("AddToCart", "WHATEVERYOURCONTROLLERISCALLED")) 

,但我認爲你缺少幾個關鍵點這裏。

有一些,你似乎忽略

ShoppingCart.cs應該命名爲ShoppingCartController.cs,並出現在你的項目的控制器文件夾

而不是命名每個價位上的模型,你可以約定使用價格選項列表並將它們作爲一系列單選按鈕顯示在表單上,​​同時進行互斥選擇。例如。

示範

public class Product{ 

// remove all the different price properties. 

// other properties go here...And while you are at it Use Pascal case for property names Eg. displaySize would be DisplaySize but I guess that is up to you. 

[Required] 
public string PriceChoice { get; set; } 

} 

的控制器

public class ShoppingCartController : Controller 
{ 

public ActionResult Details(int productId) 
{ 
    // get the product from the database 
    var model = Database.GetProductById(productId); 

    // set a default if you want 
    model.PriceChoice = "a"; 

    return View(model); 
} 

[HttpPost] 
public ActionResult AddToCart(Product model) 
{ 
    // do whatever you need to do 

    return RedirectToAction("Details", new { id = model.Id }); 
} 
} 

@using (Html.BeginForm()) 
{ 
<div>A: @Html.RadioButtonFor(x => x.PriceChoice , "a")</div> 
<div>B: @Html.RadioButtonFor(x => x.PriceChoice , "b")</div> 
@Html.ValidationMessageFor(x => x.PriceChoice) 
<input type="submit" value="OK" /> 
} 

現在,這一切都非常簡略和基本的,所以我希望你得到它的要點。

您也可以在閱讀本文時發現一些價值Post Redirect Get因此,雖然它並不嚴格適用於您正在做的事情,但它將解釋您在看到RedirectToAction的示例中正在閱讀的代碼的結構。

現在,如果你想巧妙地做到這一點,你將不得不學習一些JavaScript併發出Ajax命令。

希望這會有幫助