Asp.net mvc 3 MVC3 Actionlink与提交

Asp.net mvc 3 MVC3 Actionlink与提交,asp.net-mvc-3,radio-button,Asp.net Mvc 3,Radio Button,我是一个新的MVC用户,我正在努力使购物车如下 我试图通过actionlink传递不同价格类型的radiobutton值。 是否可以使用productId传递值? 当我点击链接时,它将调用'AddToCart'方法。 你能帮我吗?谢谢 产品模型 namespace MvcApplication2.Models { public class Product { [Key] public int productId { get; set; } public int categoryI

我是一个新的MVC用户,我正在努力使购物车如下

我试图通过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>

只需使用提交按钮而不是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>
@model mvcapapplication2.Models.Product
租期:
@使用(Html.BeginForm(“AddToCart”、“ShoppingCart”、new{id=Model.productId}、FormMethod.Post)) { @RadioButton(model=>model.price,model.threeDayPrice) 3天:£@Model.threeDayPrice @RadioButton(model=>model.price,model.aWeekPrice) 1周:£@Model.aWeekPrice @Html.radiobutton(model=>model.price,@model.twoWeekPrice) 2周:£@Model.twoWeekPrice @Html.radiobutton(model=>model.price,@model.twoWeekPrice) 1个月:£@Model.twoWeekPrice @Html.radiobutton(model=>model.price,@model.threeMonthPrice) 3个月:£@Model.threeMonthPrice @Html.radiobutton(model=>model.price,@model.sixMonthPrice) 6个月:£@Model.sixMonthPrice 添加到购物车 }
因为你一开始就说你是新来的,所以我要告诉你,你现在采取的方式并不是实现你想要实现的目标的最佳方式

如果将表单顶部更改为“提交”,则在表单底部放置“提交”按钮将获得要发布的数据并绑定到产品

@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" />
}
@使用(Html.BeginForm())
{
A:@Html.radioButton(x=>x.PriceChoice,“A”)
B:@Html.radiobutton(x=>x.PriceChoice,“B”)
@Html.ValidationMessageFor(x=>x.PriceChoice)
}
这些都是非常简略和基本的,所以我希望你能理解要点

此外,您会在阅读中发现一些价值,因此,虽然它并不严格适用于您正在做的事情,但它将在您看到RedirectToAction的示例中解释您正在阅读的代码的结构

现在,如果您想非常聪明地做到这一点,您必须学习一些javascript并发出Ajax命令


希望这有帮助

谢谢您的回复。当我使用submit按钮而不是actionlink时,shoppingCartController无法获取int id。它无法传递productId。在“MVCAPApplication2.Controllers.ShoppingCartController”“@wholee1”中出现错误,“参数字典包含不可为null类型的参数“id”System.Int32“方法”System.Web.Mvc.ActionResult AddToCart(Int32)”,然后在生成表单时添加id:
@using(Html.BeginForm)(“AddToCart”,“ShoppingCart”,新的{id=Model.productId},FormMethod.Post))
。我更新了我的答案,以举例说明。我还删除了您不再需要的隐藏字段。谢谢,它现在正在传递productId。非常感谢你!
 @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" />
}