SelectListItem未设置选定值

时间:2015-04-06 18:14:50

标签: c# asp.net-mvc linq asp.net-mvc-4

我有这些课程。

public class Store {
  // constructor
  public int Id { get; set; }
  public string Name { get; set; }

  public virtual ICollection<Stock> Stocks { get; set; }
}

public class Stock {
  public int Id { get; set; }
  public int StoreId { get; set; }
  public int ProductId { get; set; }

  public virtual Store Store { get; set; }
  public virtual Product Product { get; set; }
}

public class Product {
  // constructor
  public int Id { get; set; }
  public string Name { get; set; }

  public ICollection<Stock> Stocks { get; set; }
}

public class Order {
  // constructor
  public int Id { get; set; }

  public virtual ICollection<OrderItem> OrderItems { get; set; }
}

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

  public ICollection<Store> Stores { get; set; }
  public Item Item { get; set; }
  public Order Order { get; set; }
}

流体api:

public class OrderItemMap : EntityTypeConfiguration<OrderItem> {
  public OrderItemMap() {
    HasMany(i => i.Stores)
      .WithMany()
      .Map(m => {
          m.MapLeftKey("OrderItemId");
          m.MapRightKey("StoreId");
          m.ToTable("FromStore");
        });
  }
}

现在,例如,我已经创建了一个订单并设置了我的详细信息以及商品商店,了解他们应该将商品送到哪里。 注意:商品可以有多个商店

我的问题是..如果用户想要修改订单。我不知道如何将商店列表显示为下拉列表及其选定的值。

这是我到目前为止所做的事情:

var order = _db.Orders.Find(orderId);
var orderVm = new EditOrderVm {
  OrderId = order.Id,
  OrderItems = order
               .OrderItems
               .Select(o => new EditOrderItemVm {
                 OrderItemId = o.Id,
                 OrderId = o.OrderId,
                 ProductId = o.ProductId,
                 Stores = (from sc in o.Product.Stocks
                           from st in o.Stores.DefaultIfEmpty()
                           select new {
                             Id = sc.Store.Id,
                             Name = sc.Store.Name
                           }).ToSelectList(s => s.Name, s => s.Id, s => true)
               })
}

我的虚拟机:

public class EditOrderItemVm {
  // removed some for brevity
  public IEnumerable<SelectListItem> Storses { get; set; }
}

我的助手(我的是我的)

public static IEnumerable<SelectListItem> ToSelectList<T, TTextProperty, TValueProperty>(this IEnumerable<T> instance, Func<T, TTextProperty> text, Func<T, TValueProperty> value, Func<T, bool> selectedItem = null)
        {
            return instance.Select(t => new SelectListItem
            {
                Text = Convert.ToString(text(t)),
                Value = Convert.ToString(value(t)),
                Selected = selectedItem != null && selectedItem(t)
            });
        }

我的观点:

@Model.OrderNumber
@foreach(var item in Model.OrderItems) {

    @Html.DropDownListFor(model => item.StoreId, item.Stores, "Select store")
    @Html.ValidationMessageFor(model => item.StoreId)
}

通过上面的查询,我可以使用商店列表填充下拉列表,了解产品的存储位置,但我不知道如何设置在创建订单时选择的内容。

如果我可以使用商店列表填充下拉列表以了解项目的存储位置并将其设置为所选值,那将会很棒。

2 个答案:

答案 0 :(得分:1)

此行应如下所示,您必须从数据库中获取所有商店并选择参与当前订单的商家。

var order = _db.Orders.Find(orderId);
var stores = _db.Stores; // take all stores from DB
var orderVm = new EditOrderVm {
  OrderId = order.Id,
  OrderItems = order
               .OrderItems
               .Select(o => new EditOrderItemVm {
                 OrderItemId = o.Id,
                 OrderId = o.OrderId,
                 ProductId = o.ProductId,
                 Stores = (from st in stores // all stores
                           select new {
                             Id = st.Id,
                             Name = st.Name
                        }).ToSelectList(
                            s => s.Name,
                            s => s.Id, 
                            s => o.Stores.Any(sto => sto.Id == s.Id) //select only existing stores
                        )

               })
}

答案 1 :(得分:0)

而不是s => true,你必须这样做 s => s.Id == YOUR_CURRENT_STORE_Id