接收错误消息:System.Reflection.TargetException:非静态方法需要目标

时间:2014-11-14 06:53:54

标签: c# asp.net-mvc-5 unhandled-exception

我正在使用Visual Studio 2013 Update 2,我在我的一个控制器中遇到了未处理的异常,我无法确定它来自何处。 这是控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using Microsoft.AspNet.Identity;

namespace SophicCeramics.Controllers
{
public class ShopController : BaseController
{
public ActionResult Cart()
    {
        var order = FindOrCreateOrder();
        var m = new Models.Order.Cart();  //
        m.Date = order.OrderDate;
        m.Total = order.OrderLineItems.Sum(i => i.Quantity * i.UnitPrice);
        m.ItemCount = order.OrderLineItems.Count;
        m.Items = order.OrderLineItems.ToList();
        return View(m);
    }
    public async Task<ActionResult> AddItem(int productId)
    {
        var product = _context.Products.FirstOrDefault(p => p.ProductId == productId);
        var order = FindOrCreateOrder();
        var item = order.OrderLineItems.FirstOrDefault(i => i.ProductId == productId);
        if (item != null)
        {
            item.Quantity++;
            await _context.SaveChangesAsync();
        }
        else
        {
            item = new OrderLineItem();
            item.OrderHeader = order;
            item.Product = product;
            item.Quantity = 1;
            item.UnitPrice = product.Price;
            order.OrderLineItems.Add(item);
            await _context.SaveChangesAsync();
        }
        return RedirectToAction("Cart");
    }
    private OrderHeader FindOrCreateOrder()
    {
        var UserName = this.User.Identity.Name;
        var user = UserManager.FindByName(UserName);
        var customer = _context.Customers
            .Include(c => c.OrderHeaders.Select(o => o.OrderLineItems.Select(i => i.Product))).
            FirstOrDefault(c => c.UserId.Equals(user.Id));

        var order = customer.OrderHeaders
            .OrderBy(o => o.OrderDate)
            .LastOrDefault(o => o.Paid == false);

        if(order == null)
        {
            order = new OrderHeader();
            //var Customer = new Customer();
            order.Customer = customer;
            customer.OrderHeaders.Add(order);
            _context.SaveChanges();
        }
        return order;
    }
  }
}

这是视图模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SophicCeramics.Models.Order
{
    public class Cart
    {
    public int ItemCount { get; set; }
    public DateTime Date { get; set; }
    public decimal Total { get; set; }
    public List<OrderLineItem> Items { get; set; }
    public decimal UnitPrice { get; set; }
    public int OrderHeaderId { get; set; }
    public string CustomerId { get; set; }
    public System.DateTime OrderDate { get; set; }
    public bool Paid { get; set; }
    public int OrderLineItemId { get; set; }
    public int ProductId { get; set; }
    public int Quantity { get; set; }
    public int CategoryId { get; set; }
    public int ArtistId { get; set; }
    public string Title { get; set; }
    public decimal Price { get; set; }
    public string ProductArtUrl { get; set; }
    public string ProductArtMain { get; set; }
    public string UserId { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    }
}

这就是观点:

@model SophicCeramics.Product
@{ ViewBag.Title = "Cart"; }
<div>
<h4>Product</h4>
<hr />
<dl class="dl-horizontal">
    <dt>
        @Html.DisplayNameFor(model => model.Title)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Title)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.Price)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Price)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.ProductArtUrl)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.ProductArtUrl)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.ProductArtMain)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.ProductArtMain)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.Artist.Name)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Artist.Name)
    </dd>
    <dt>
        @Html.DisplayNameFor(model => model.Category.Name)
    </dt>
    <dd>
        @Html.DisplayFor(model => model.Category.Name)
    </dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.ProductId }) |
@Html.ActionLink("Back to List", "Index")
</p>

View具有自动布局页面。 我不断收到指向这行代码的错误:

var customer = _context.Customers
        .Include(c => c.OrderHeaders.Select(o => o.OrderLineItems.Select(i => i.Product))).
        FirstOrDefault(c => c.UserId.Equals(user.Id));

无论我如何重新排列include语句,它都无法正常工作。 此外,控制器继承自另一个名为BaseController的控制器,该控制器通过_context连接到数据库。 我也尝试过添加edmx中.tt文件中相关类的所有内容,但仍然会出错。 此外,当我在客户变量上设置断点并逐步执行它时,它会直接转到BaseController中的dispose方法。

1 个答案:

答案 0 :(得分:0)

我猜测user为空,所以在这里访问lambda表达式中的user.Id时会出现错误

var customer = _context.Customers
    .Include(c => c.OrderHeaders.Select(o => o.OrderLineItems
    .Select(i => i.Product)))
    .FirstOrDefault(c => c.UserId.Equals(user.Id));

您需要检查此行后user是否为空

var user = UserManager.FindByName(UserName);

并且仅在FindOrCreateOrder非空时才执行user方法内的其余代码。如果user块中的else为空,您还需要决定该怎么做,如下所示

private OrderHeader FindOrCreateOrder()
{
    var UserName = this.User.Identity.Name;
    var user = UserManager.FindByName(UserName);
    if (user != null)
    {
        var customer = _context.Customers
            .Include(c => c.OrderHeaders.Select(o => o.OrderLineItems.Select(i => i.Product))).
            FirstOrDefault(c => c.UserId.Equals(user.Id));

        var order = customer.OrderHeaders
            .OrderBy(o => o.OrderDate)
            .LastOrDefault(o => o.Paid == false);

        if(order == null)
        {
            order = new OrderHeader();
            //var Customer = new Customer();
            order.Customer = customer;
            customer.OrderHeaders.Add(order);
            _context.SaveChanges();
        }
        return order;
    }
    else
    {
        // logic when user is null
    }
}