0

有人可以向我解释如何在单个视图中使用多个模型,其中每个模型代表一个数据库表吗?

我目前所做的是为每个模型创建一个模型文件。

示例模型:

[Table("Order")]
public class OrderModel
{
    [Key, Column(Order = 0)]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int OrderID { get; set; }

    [Key, Column(Order = 1)]
    public int UserID { get; set; }
    public UserProfile Account { get; set; }

    public DateTime Date { get; set; }
    public int ShipLocation { get; set; }
    public string PONumber { get; set; }

    public int StatusID { get; set; }
    public StatusModel Status { get; set; }
}

这是包含在单个控制器/视图中使用的所有模型的另一个模型。

public class OrderPlacementModel
{
    public OrderModel OrderChild { get; set; }
    public OrderItemsModel OrderItemsChild { get; set; }
    public StatusModel StatusChild { get; set; }
    public MaterialsModel MaterialsChild { get; set; }
    public CategoryModel CategoryChild { get; set; }
    public PackModel PackChild { get; set; }
}
4

2 回答 2

1
public ActionResult PlaceOrder()
{
  var viewModel = new OrderPlacementModel
  {
    OrderChild = new OrderModel(),//or fetch this object from your data source
    OrderItemsChild = new OrderItemsChild(),
    //...etcetera
  };
  return View(viewModel);
}

编辑

或者,如果您将视图强输入到 aList<OrderPlacementModel>而不是单个实例,则可以执行类似以下操作:

public ActionResult PlaceOrder()
    {
      var viewModel = new List<OrderPlacementModel>();
      var model = new OrderPlacementModel
      {
        OrderChild = new OrderModel(),//or fetch this object from your data source
        OrderItemsChild = new OrderItemsChild(),
        //...etcetera
      };
      viewModel.Add(model);
      //lather, rinse, repeat for however many instances you need to send to your view.
      return View(viewModel);
    }
于 2013-02-04T19:01:28.117 回答
0

Ideally, you should create a view model for the view that encompasses the fields from each model that you need to expose via the view. You can then map these in your controller. I would keep your mapping classes completely ignorant of your view models. Keep your views independent of your data model.

public class OrderViewModel
{
    public int OrderId { get; set; }
    public int UserId { get; set; }
    public DateTime Date { get; set; }
    public int ShippingLocation { get; set; }

    public List<ItemViewModel> Items { get; set; }
}

public class ItemViewModel
{
    public int ItemId { get; set; }
    public int Title { get; set; }
}

Note how I have created a view model for the order and - to allow the order have multiple items - have separated these out into a separate model class. Now, you can type your view to OrderViewModel and use as many instances of ItemViewModel as your require.

You can then map your viewmodels to database entities from your controller:

[HttpPost]
public ActionResult ConfirmOrder (OrderViewModel model)
{
    if (ModelState.IsValid)
    {
        foreach (ItemViewModel item in model.Items)
        {
            /* Create instance of OrderItemsModel (or whatever your
            DB mapping class is), populate with appropriate data
            from 'item' and commit to database. */
        }

        OrderModel order = new OrderModel();
        order.OrderId = model.OrderId;
        order.UserId = model.UserId;
        order.Date = model.Date;
        order.ShipLocation = model.ShippingLocation;

        /* TODO: Commit new order to database */

    }
}

Doing things this way adds a little overhead to your initial development time but allows you a great deal more flexibility as you aren't forced to mould all of your views to the shape of your entity classes.

于 2013-02-04T21:19:10.300 回答