MVC 4 EF Html.BeginForm没有传递多个参数

时间:2016-06-05 11:19:55

标签: c# asp.net-mvc entity-framework

我在让Html.BeginForm传递多个参数方面遇到了一些麻烦。我需要一次将任意数量的相同项目添加到心愿单,所以我试图将id和金额传递给控制器​​。

我的观点:

@using Website.Models
@using System.Text.RegularExpressions
@model Website.Models.Film

@{
    ViewBag.Title = Model.Name;
 }
@if (Model != null)

//some code

        using (Html.BeginForm("AddToWishlist", "Wishlist", new { eventId = Model.Id, amount = Model.ticketAmount }, FormMethod.Post))
        {
        <p>Tickets: </p>
            Html.DropDownListFor(m => m.ticketAmount, Film.GetFilmSelectItems());
        }

        <input type="button" value="Add to wishlist" @ViewBag.Message/>
    }

else
    {
       <h1><b>@ViewBag.Error</b></h1>      
    }

我的模特:

namespace Website.Models
{
    [Table("Film")]
    public class Film : Item
    {

        public string Genre { get; set; }
        public string ImdbLink { get; set; }

        [NotMapped]
        public int ticketAmount;

        public Film(int id, string name, string description_en, string description_nl, decimal price, string genre, int locationId)
            : base(id, name, description_en, description_nl, price, locationId)
        {
            this.Genre = genre;
        }

        public Film()
        {

        }

        public static IEnumerable<SelectListItem> GetFilmSelectItems()
        {
            const int MAXAMOUNTTICKETS = 10;
            yield return new SelectListItem { Text = "1 ticket", Value = "1" };
            for (int i = 2; i <= MAXAMOUNTTICKETS; i++)
            {
                yield return new SelectListItem { Text = String.Format("{} tickets", i), Value = i.ToString() };
            }
        }

WishlistController:

[HttpPost]
        public ActionResult AddToWishList(int eventId, int amount)
        {
            cookieCheck.CheckCookie();
            int wishlistId = int.Parse(Request.Cookies["WishList"]["WishlistId"]);
            WishlistItem items = new WishlistItem(wishlistId, eventId, amount);
            wishlistRepo.AddItem(items);
            return RedirectToAction("Index");
        }

如果我运行它并尝试添加一些东西,&#39; eventId&#39;是正确传递但是&#39;金额&#39;一片空白。是否无法传递多个参数,或者下拉列表是否未传递值?

1 个答案:

答案 0 :(得分:0)

您可以尝试将下拉列表的名称设置为“金额”,以便它包含在您的帖子中。

类似的东西:

Html.DropDownListFor(m => m.ticketAmount, Film.GetFilmSelectItems(), new { @name="amount"});