C#visual studio asp.net将项添加到列表属性NullAttribute异常:

时间:2015-10-13 05:46:38

标签: c# html asp.net asp.net-mvc visual-studio-2013

我目前正在开展一个模拟自行车商店的项目。在我的订单'对象,我在订单上有自行车项目的列表对象。感谢This question,我几乎实现了一个selectList,我可以在一个订单中添加一个或多个自行车。但是,我在Post Controller方法中的以下代码行遇到了ArgumentNullException:

IEnumerable<int> selectedItems = model.Inventory.Where(i => i.IsSelected).Select(i => i.Id);

任何人都可以告诉我为什么会这样,以及如何解决它?

我的创建视图:

 <div class="form-group">
            @foreach(var i in Model.Inventory)
            {
            @Html.HiddenFor(m => m.Inventory.First(x=>x.Id==i.Id).Id)
            @Html.CheckBoxFor(m => m.Inventory.First(x => x.Id == i.Id).IsSelected)
            @Html.LabelFor(m => m.Inventory.First(x => x.Id == i.Id).IsSelected, Model.Inventory.First(x => x.Id == i.Id).Name)
            }
            @Html.TextBoxFor(m => m.PaymentMethod)
            <input type="submit" value="Create" />
        </div>

我的订单查看模型:

namespace BikeStore.Models
{
    public class OrderVM
    {
        public string PaymentMethod { get; set; }
        public IEnumerable<InventoryVM> Inventory { get; set; }
    }
}

我的广告资源视图模型: 命名空间BikeStore.Models

{
    public class InventoryVM
    {

        public int Id { get; set; }
        public string Name { get; set; }
        public bool IsSelected { get; set; }
    }
}

我的控制器方法:

// GET: Orders/Create
        public ActionResult Create()
        {
            var inventory = db.StoreInventory;
            OrderVM model = new OrderVM
            {
                Inventory = (IEnumerable<InventoryVM>)inventory.Select(i => new InventoryVM { Id = i.Id, Name = i.Model })
            };
            return View(model);
        }

我的后期控制器方法:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "PaymentMethod, Inventory")]OrderVM model)
        {
            var Order = new Order
            {
                CustomerName = User.Identity.Name,
                OrderDate = DateTime.Now,
                PaymentMethod = model.PaymentMethod,
                TotalCost=0

            };
            IEnumerable<int> selectedItems = model.Inventory.Where(i => i.IsSelected).Select(i => i.Id);
            foreach(var item in selectedItems)
            {
      // You have not shown the model for this so just guessing
                var orderItem = new OrderedItem { OrderId = Order.OrderNumber, InventoryId = item };
                db.OrderedItems.Add(orderItem);
            }
            db.SaveChanges();
            return View(model);

        }

我的OrderedItem模型:

namespace BikeStore.Models
{
    public class OrderedItem
    {
        [Key, DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int OrderId { get; set; }
        public int InventoryId { get; set; }
    }
}

我的订单型号:

public class Order
    {
        public Order()
        {
            OrderedItems = new List<Inventory>();
        }

        public string CustomerName { get; set; } //FROM CONTROLLER User.Identity.Name

        public virtual List<Inventory> OrderedItems { get; set; }
        //public virtual List<Account> Accounts { get; set; }
        public DateTime OrderDate { get; set; }

        public DateTime PickupDate { get; set; }

         [Key, DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int OrderNumber { get; set; }

        public Store StoreOrderedFrom { get; set; }

        public Decimal TotalCost { get; set; }

        public string PaymentMethod { get; set; }
}

0 个答案:

没有答案