如果条件在Razor视图中失败,则抛出错误"序列不包含匹配元素"

时间:2015-12-16 01:07:09

标签: asp.net-mvc

在我的项目中,我有一个动作方法,下面附有我遇到问题的视图部分。

问题在于View Model中的IF条件.Transaction.count()> 0,如果条件为true则没有问题,但如果条件失败(事务计数为0)则抛出异常&#0 34;序列不包含匹配元素"

请有人帮助我.. !!

 public ActionResult Details(int id)
    { 
        Order order = db.Orders.Find(id);
        try
        {

            order.OrderItems = db.OrderItems.Where(e => e.OrderID == id).ToList();

            int EventID = int.Parse(ConfigurationManager.AppSettings["VirtualEventID"]);

            order.Ticketservices = db_Tradevent.Ticketservices.Where(t => t.TEEventID == EventID)
                                                                .Where(t => t.EntityID == order.OrderID)
                                                                .ToList();

            order.Transactions = db_Tradevent.TrackerTransactions.Where(t => t.TEEventID == EventID)
                                                                .Where(t => t.OrderID == order.OrderID)
                                                                .Where(t => t.TransactionStatusID == 2).ToList();


            bool HasERT = false;

            foreach (var orderitem in order.OrderItems)
            {
                orderitem.Trackers = db.Trackers.Where(e => e.OrderItemID == orderitem.OrderItemID).ToList();

                foreach (var tracker in orderitem.Trackers)
                {
                    tracker.Qualifiers = db.Qualifiers.Where(e => e.TrackerID == tracker.TrackerID).OrderBy(s => s.QualifierID).ToList();
                }

                if (orderitem.EventProduct.Product.Type == "ERT")
                {
                    HasERT = true;
                }
            }

            @ViewBag.HasERT = HasERT;
        }
        catch(Exception ex){

        }

        return View(order);
    }enter code here

我遇到问题的部分视图如下

@if (Model.OrderStatusID == 3 && Model.Transactions.Count() > 0)
    {
        if (Model.Transactions.SingleOrDefault(t => t.OrderID == Model.OrderID).TransactionMethodID == 2)
        {
            <tr>
                <td align="right">
                    Surcharge
                </td>
                <td colspan="@(ViewBag.HasERT ? 3 : 2)" style="color: Green; text-align: right">
                    @(Model.Transactions.Single(t => t.OrderID == Model.OrderID).TransactionAmount - (decTotal / 10 + decTotal))
                </td>
            </tr>
        }
    }

0 个答案:

没有答案