通过实体框架结合多种模型创建新记录

时间:2019-01-04 22:42:48

标签: c# asp.net-mvc entity-framework

我创建了一个具有多个模型的视图(通过ViewModel)来显示多个表中的数据(即帐单地址,送货地址,订单的订单项)。在我的示例中,我想单击“提交”按钮以基本下订单,在该订单中,仅一部分信息需要传递并存储到新表(订单)中,假设引用了帐单地址ID以保持很简单。

第二步,我将尝试获取在订单表中创建的新行的ID,以更新另一个行(订单项表)。但是,由于某种原因,我什至不仅仅可以在orders表中创建记录,因此我们非常感谢您的帮助!

在我的控制器下面-直到ModelState验证生效,一切都中断了,并给了我一个“对象引用未设置为对象实例”的错误 ...

// POST: /Shop/CreateOrder
[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SubmitOrder(CreateOrderViewModel model)
{
// define variables
var userID = User.Identity.GetUserId();
DateTime nowUTC = DateTime.Now.ToUniversalTime();
DateTime nowLocal = DateTime.Now.ToLocalTime();
int NumberOfLineItems = DATADB.LineItemList.Where(x => x.OrderNumber == 0 && x.UserID == userID).Count();
decimal OrderAmount = DATADB.LineItemList.Where(x => x.OrderNumber == 0 && x.UserID == userID).Sum(x => x.TotalPrice);
if (ModelState.IsValid)
{
    var order = new Orders
    {
        UserID = userID,
        ShippingAddressID = model.ShippingAddressModel.ID,
        NumberOfLineItems = NumberOfLineItems,
        OrderAmount = OrderAmount,
        PaymentMethodID = model.PaymentMethodModel.ID,
        OrderDate = nowUTC,
        OrderLatitude = model.OrdersModel.OrderLatitude,
        OrderLongitude = model.OrdersModel.OrderLongitude,
        OrderLocation = model.OrdersModel.OrderLocation
    };
    DATADB.OrderList.Add(order);
    DATADB.SaveChanges();
    int orderID = order.ID;
    var lineItems = DATADB.LineItemList.Where(x => x.OrderNumber == 0 && x.UserID == userID);
    lineItems.ForEach(l => l.OrderNumber = orderID);
    DATADB.SaveChanges();
    return View("Home", "Home");
}
else
{
    return View(model);
}
}

视图模型如下:

public class CreateOrderViewModel
{
    public Orders OrdersModel { get; set; }
    public LineItems LineItemModel { get; set; }
    public IEnumerable<LineItems> LineItemListModel { get; set; }
    public ShippingAddressesViewModel ShippingAddressModel { get; set; }
    public ProfileInformationViewModel ProfileInformationModel { get; set; }
    public PaymentMethods PaymentMethodModel { get; set; }
}

...和这样的订单模型:

public class Orders
{
    public int ID { get; set; }
    public string UserID { get; set; }
    public int ShippingAddressID { get; set; }
    public int NumberOfLineItems { get; set; }
    public int PaymentMethodID { get; set; }
    public decimal OrderAmount { get; set; }
    public decimal ShippingCost { get; set; }
    public DateTime OrderDate { get; set; }
    public string OrderLatitude { get; set; }
    public string OrderLongitude { get; set; }
    public string OrderLocation { get; set; }
}

1 个答案:

答案 0 :(得分:3)

您的视图模型CreateOrderViewModel中包含多个类。在以下代码中,检查null-object scenarios

if (ModelState.IsValid)
{
    //Check if model.ShippingAddressModel is empty
    //Check if model.PaymentMethodModel is empty
    //Check if model.OrdersModel is empty

    var order = new Orders
    {
        UserID = userID,
        ShippingAddressID = model?.ShippingAddressModel?.ID,
        NumberOfLineItems = NumberOfLineItems,
        OrderAmount = OrderAmount,
        PaymentMethodID = model?.PaymentMethodModel?.ID,
        OrderDate = nowUTC,
        OrderLatitude = model?.OrdersModel?.OrderLatitude,
        OrderLongitude = model?.OrdersModel?.OrderLongitude,
        OrderLocation = model?.OrdersModel?.OrderLocation
    };

   //
   //
}