2012-03-18 47 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命令。

希望这会有帮助