在MVC中发送选项值给FormCollection的控制器
问题描述:
我想通过FormCollection向Controller发送选项值,但我只能在FormCollection数组中获取选择名称信息。这里的错在哪里?谢谢你的帮助。在MVC中发送选项值给FormCollection的控制器
这是我的看法(这里的型号是产品);
@using (Html.BeginForm("AddToCart", "Home", new { id = Model.Id }, FormMethod.Post))
{
<div class="product-sides-part">
<div class="product-sides-part-itemcontainer">
@if (Model.Size == "0")
{
<select name="Drink1">
@foreach (var item in ViewBag.Drinks)
{
<option id="Drinks1" value="value">@item.Name</option>
}
</select>
}
这是我的控制器;
public ActionResult AddToCart(FormCollection formCollection, int id, string root, int quantity = 0)
{
if (root == "cart" || root == "Checkout")
{
if (Session["cartpastahemen"] == null)
{
Cart s = new Cart();
Product eklenecek = db.Products.Find(id);
switch (eklenecek.Size)
{
case"0":
s.AddToCart(new CartProduct
{
Drinks1=formCollection["Drinks1"],
Drinks2=null,
Drinks3=null,
Cips1 = formCollection["Cips1"],
Cips2=null,
Cips3=null,
Snack1 = formCollection["Snack1"],
Snack2=null,
Snack3=null,
Size=eklenecek.Size,
ProductId = eklenecek.Id,
Category = eklenecek.Category.Name,
Quantity = quantity,
Price = eklenecek.DiscountedPrice,
PhotoPath = eklenecek.Photos.FirstOrDefault() != null ? eklenecek.Photos.FirstOrDefault().PhotoPath : "Resim_yok.gif",
Name = eklenecek.Name,
});
break;
答
我已经解决了。我写错了value =“value”而不是值“@ item.name”。我已经改变,它已经解决了。