2015-10-13 69 views
0

我目前正在一个项目中建模一个自行车店。在我的'订单'对象中,订单上的自行车项目有一个lis对象。我如何将自行车添加到这个列表中? I.E我想在“创建”视图中显示可用自行车列表,并在订单中添加一个或多个自行车。C#visual studio asp.net将一个项目添加到列表属性

我的控制器:

 public ActionResult Create() 
     { 
      return View(); 
     } 


     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Create([Bind(Include = "OrderNumber,CustomerName,OrderDate,PickupDate,TotalCost,PaymentMethod")] Order order) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Orders.Add(order); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      return View(order); 
     } 

我的库存模型

public class Inventory 
    { 
     public int Id { get; set; } 

     public string SerialNumber { get; set; } 

     public virtual Store Store { get; set; } 
     public int? StoreId { get; set; } 

     public string Model { get; set; } 

     public string Description { get; set; } 

     public Decimal InventoryCost { get; set; } 

     public Decimal RecSalePrice { get; set; } 

     public Decimal SalePrice { get; set; } 

     public string PaymentMethod { get; set; } 

     public virtual BikeCategory Category { get; set; } 
     public int? CategoryId { get; set; } 







    }  

我的订单模式:

namespace BikeStore.Models 
{ 

    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; } 


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

在订单的创建视图:

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>Order</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.CustomerName, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.CustomerName, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.CustomerName, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.OrderDate, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.OrderDate, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.OrderDate, "", new { @class = "text-danger" }) 
      </div> 
     </div> 


     <div class="form-group"> 
      @Html.LabelFor(model => model.PickupDate, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.PickupDate, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.PickupDate, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.TotalCost, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.TotalCost, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.TotalCost, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.PaymentMethod, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.PaymentMethod, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.PaymentMethod, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 
+1

不要将实体框架的实体模型类用作ViewModels。尽管他们的名字都是“模范”,但这两个概念是截然不同的概念。 – Dai

+0

您究竟在哪里看到ViewModel? –

+0

不清楚你的问题是。您是否想在“订单”视图中显示可用自行车的列表,以便您可以选择一个或多个以包含在订单中?所有这些代码的相关性是什么?你用什么'Delete()'方法来处理这个问题? –

回答

0

开始创建视图模型来表示要在视图中显示/编辑什么(添加显示器和适当的验证属性)

public class InventoryVM 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public bool IsSelected { get; set; } 
} 
public class OrderVM 
{ 
    public string PaymentMethod { get; set; } 
    public List<InventoryVM> Inventory { get; set; } 
} 

注意CustomerNameOrderDateTotal是不恰当的(你不” t希望用户编辑这些内容 - 它们应在保存订单前立即在POST方法中设置)。不知道PickupDate代表什么,但是如果它的实际日期那么这也不合适(它将在订单收集时单独设置)。我还建议PaymentMethod是一个枚举或集合PaymentType's,并且您在视图中使用下拉列表进行选择。

那么GET方法是

public ActionResult Create() 
{ 
    // Get all available bikes, for example 
    var inventory = db.Inventory; 
    OrderVM model = new OrderVM 
    { 
    Inventory = inventory.Select(i => new 
    { 
     ID = i.ID, 
     Name = i.Model // modify this to suit what you want to display in the view 
    }).ToList() 
    }; 
    return View(model); 
} 

并在视图

@model yourAssembly.OrderVM 
@using (Html.BeginForm()) 
{ 
    for(int i = 0; i < Model.Inventory.Count; i++) 
    { 
    @Html.HiddenFor(m => m.Inventory[i].ID) 
    @Html.CheckBoxFor(m => m.Inventory[i].IsSelected) 
    @Html.LabelFor(m => m.Inventory[i].IsSelected, Model.Inventory[i].Name) 
    } 
    @Html.TextBoxFor(m => m.PayentMethod) 
    <input type="submit" value="Create" /> 
} 

而且POST方法是

public ActionResult Create(OrderVM model) 
{ 
    // Initialize a new Order and map properties from view model 
    var order = new Order 
    { 
    CustomerName = User.Identity.Name, 
    OrderDate = DateTime.Now, 
    .... 
    PaymentMethod = model.PaymentMethod 
    } 
    // Save the order so that you now have its `ID` 
    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 OrderItem{ OrderID = order.Id, InventoryId = item }; 
    db.OrderItems.Add(orderItem); 
    } 
    db.SaveChanges(); 
} 

旁注:

  1. 如果你希望能够让用户选择更多,任何 项目之一,你可以改变bool IsSelectedint Quantity
  2. 如果你想显示有关项目的其他信息, 说DescriptionCost你可包括附加的属性在InventoryVM视图模型 ,如果你想显示在 视图中的所有选择项的总成本与 @Html.DisplayFor(m => m.Inventory[i].Description)
  3. 显示它们,你需要使用JavaScript/jQuery的
  4. 如果ModelState可能是无效的,则需要重新填充的InventoryVM的 属性返回视图前视图(如图所示 只有IDIsSelected性质回来后),或包括 其他属性的隐藏输入
+0

谢谢!但是,I.m在IEnumerable 处得到运行时错误selectedItems = order.Inventory.Where(i => i.IsSelected);我一直在那里得到一个ArgumentNullException。你会碰巧知道这是怎么回事? –

+0

糟糕 - '.Select(i => i.ID)'位的左侧 - 参见编辑(并且它是'IEnumerable '不'IEnumerable ' –

+0

及其'型号。库存'而非'order.Inventory' –

相关问题