【问题标题】:C# visual studio asp.net adding an item to a list attributeC# Visual Studio asp.net 将项目添加到列表属性
【发布时间】:2015-10-13 00:51:14
【问题描述】:

我目前正在开展一个为自行车商店建模的项目。在我的“订单”对象中,我有一个 lis 对象用于订单上的 Bike 项目。我如何将自行车添加到此列表中?即我想在创建视图中显示可用自行车的列表,然后将其中一辆或多辆添加到订单中。

我的控制器:

        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>

【问题讨论】:

  • 不要使用实体框架的实体模型类作为视图模型。尽管它们的名称中都有“模型”,但这两者是不同的概念。
  • 您在哪里看到 ViewModel?
  • 不清楚你的问题是什么。您是否想在Order 视图中显示可用自行车列表,以便您可以选择其中一辆以包含在订单中?所有这些代码的相关性是什么?例如,您Delete() 方法与该问题有什么关系?
  • @StephenMuecke 是的,没错。
  • 首先从您的问题中删除所有不相关的代码,我们可能会倾向于查看它:)

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


【解决方案1】:

首先创建视图模型来表示您想要在视图中显示/编辑的内容(根据需要添加显示和验证属性)

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 是不合适的(您不希望用户编辑这些 - 应该在保存订单之前立即在 POST 方法中设置它们)。不确定PickupDate 代表什么,但如果它是实际日期,那么这也不合适(它会在收集订单时单独设置)。我还建议 PaymentMethodPaymentType 的枚举或集合,并在视图中使用下拉列表进行选择。

那么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 IsSelected改成int Quantity
  2. 如果您想显示有关项目的其他信息, 说DescriptionCost 你可以包含其他属性 在InventoryVM 视图模型中显示它们 @Html.DisplayFor(m =&gt; m.Inventory[i].Description)
  3. 如果要显示所有选定项目的总成本 查看,您将需要使用 javascript/jquery
  4. 如果ModelState 可能无效,您将需要重新填充 返回视图之前InventoryVM的属性(如图 只有IDIsSelected 属性回发)或​​包括 视图中其他属性的隐藏输入

【讨论】:

  • 谢谢!但是,我在 IEnumerable selectedItems = order.Inventory.Where(i => i.IsSelected); 处遇到运行时错误我一直在那里收到 ArgumentNullException。你会碰巧知道这是怎么回事吗?
  • 糟糕 - .Select(i =&gt; i.ID) 位的左侧 - 请参阅编辑(它是 IEnumerable&lt;int&gt; 而不是 IEnumerable&lt;InventoryVM&gt;
  • 它的model.Inventory 不是order.Inventory
  • 似乎可以工作,但仍然收到 ArgumentNullException,同一个地方。知道可能出了什么问题吗?
猜你喜欢
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2011-09-11
  • 1970-01-01
  • 2022-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多