如何修复.NET中的“ System.Reflection.TargetException”错误

时间:2019-07-09 07:28:00

标签: c# .net

为什么当我回到页面时会弹出错误System.Reflection.TargetException:“非静态方法需要目标对象。”

public ActionResult HistoryOrder(int id)
{
    var orderList = db.OrdersItems.SingleOrDefault(x => x.OrderItemsId == id);

    orderList.IsHistory = true;
    db.SaveChanges();

    return RedirectToAction("ListYourProduct");
}
public ActionResult ListYourProduct(string userId)
{
    var user = UserManager.FindById(userId);

    var orderItems = db.OrdersItems.Where(x => x.UserName == user.UserName && x.IsHistory == false).ToList();

    return View(orderItems);
}
<a href="@Url.Action("HistoryOrder", "Order", new { id = item.OrderItemsId, userId = User.Identity.GetUserId()})">Archiwizuj</a>

2 个答案:

答案 0 :(得分:0)

从链接中的路由中删除userId = User.Identity.GetUserId()并通过

在控制器中获取它
public ActionResult ListYourProduct()
{
    var userId = User.Identity.GetUserId();
    var user = UserManager.FindById(userId);

    var orderItems = db.OrdersItems.Where(x => x.UserName == user.UserName && x.IsHistory == false).ToList();

    return View(orderItems);
}

答案 1 :(得分:0)

喜欢吗?

        public ActionResult HistoryOrder(int id)
    {
        string userId = User.Identity.GetUserId();
        var orderList = db.OrdersItems.SingleOrDefault(x => x.OrderItemsId == id);

        orderList.IsHistory = true;
        db.SaveChanges();

        return RedirectToAction("ListYourProduct",userId);
    }

<a href="@Url.Action("HistoryOrder", "Order", new { id = item.OrderItemsId})">Archiwizuj</a>