我新的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");
}
謝謝你的回覆。當我使用提交按鈕而不是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
@ wholee1,然後在生成表單時添加id:@using(Html.BeginForm(「AddToCart」,「ShoppingCart」,new {id = Model.productId},FormMethod.Post))''。我已經更新了我的答案,以舉例說明。我還刪除了不再需要的隱藏字段。 – 2012-03-18 22:05:00
謝謝你現在正在傳遞productId。非常感謝! – wholee1 2012-03-18 22:07:53