MVC3 Actionlink与提交

时间:2012-03-18 20:23:58

标签: asp.net-mvc-3 radio-button

我是新的MVC用户,我正在尝试按照MVC Music Store tutorial

制作购物车

我试图通过actionlink传递不同价格类型的radiobutton值。 是否可以通过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 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

因为你说你刚才是新人我会告诉你,你这样做的方式并不是达到你想要达到目标的最好方法。

如果您将表单顶部更改为

,则在表单底部放置一个提交按钮将获取要发布的数据并绑定到Product
@using (Html.BeginForm("AddToCart", "WHATEVERYOURCONTROLLERISCALLED"))

但我认为你在这里缺少一些关键点。

您似乎忽略了一些惯例

ShoppingCart.cs应该命名为ShoppingCartController.cs并出现在项目的controllers文件夹中

您可以使用价格列表选项并在表单上将其显示为一系列单选按钮,而不是在模型上命名每个价格,同时选择相互排斥的选项。例如。

模特

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命令。

希望这有帮助