我正在使用Visual Studio 2013 Update 2,並且在我的一個控制器中出現未處理的異常,我無法確定它的位置來自(哪裏。 這是控制器:接收錯誤消息:System.Reflection.TargetException:非靜態方法需要目標
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>
視圖已經是自動佈局頁面。 我不斷收到指着這行代碼的錯誤:
var customer = _context.Customers
.Include(c => c.OrderHeaders.Select(o => o.OrderLineItems.Select(i => i.Product))).
FirstOrDefault(c => c.UserId.Equals(user.Id));
無論我如何重新安排包括聲明它只是將無法正常工作。 此外,控制器正在繼承另一個名爲BaseController的控制器,該控制器通過_context與數據庫建立連接。 我也試過在edmx中的.tt文件的相關類中添加幾乎所有的東西,但仍然出現錯誤。 另外,當我在customer變量上放置一個斷點並逐步完成時,它會直接轉到BaseController中的dispose方法。
是'user'空當你的錯誤? – ekad
與http://stackoverflow.com/questions/13717355/non-static-method-requires-a-target相同 – Maarten