小编典典

非静态方法需要目标

c#

我有一个控制器操作,该操作在Firefox本地和生产环境中以及本地IE上均能正常运行,但在生产环境中却无法正常运行。这是我的控制器动作:

public ActionResult MNPurchase()
{
    CalculationViewModel calculationViewModel = (CalculationViewModel)TempData["calculationViewModel"];

    decimal OP = landTitleUnitOfWork.Sales.Find()
        .Where(x => x.Min >= calculationViewModel.SalesPrice)
        .FirstOrDefault()
        .OP;

    decimal MP = landTitleUnitOfWork.Sales.Find()
        .Where(x => x.Min >= calculationViewModel.MortgageAmount)
        .FirstOrDefault()
        .MP;

    calculationViewModel.LoanAmount = (OP + 100) - MP;
    calculationViewModel.LendersTitleInsurance = (calculationViewModel.LoanAmount + 850);

    return View(calculationViewModel);
}

这是我在IE中获得的堆栈跟踪:

错误。处理您的请求时发生错误。System.Reflection.TargetException:非静态方法需要一个目标。在System.Reflection.RuntimeMethodInfo.CheckConsistency(Object
target)在System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object
obj,BindingFlags invokeAttr,活页夹活页夹,Object
[]参数,CultureInfo文化)在System.Reflection.RuntimeMethodInfo.Invoke(Object obj,
BindingFlags invokeAttr,活页夹活页夹,Object
[]参数,CultureInfo文化),位于System.Reflection.RuntimePropertyInfo.GetValue(Object
obj,Object
[]索引),位于System.Data.Objects.ELinq.QueryParameterExpression.TryGetFieldOrPropertyValue(MemberExpression我,对象实例,
System.Data.Objects.ELinq.QueryParameterExpression.TryEvaluatePath(Expression
expression,1 forMergeOption) at System.Data.Objects.ObjectQuery1.GetResults(可空1 forMergeOption) at System.Data.Objects.ObjectQuery1.System.Collections.Generic.IEnumerable.GetEnumerator()在System.Linq.Enumerable.FirstOrDefault
[TSource](IEnumerable的1 source) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable1源)在LandTitle.Controllers.HomeController.MNRefi()在lambda_method(封闭,ControllerBase
,Object
[]),位于System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext
controllerContext,IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2个参数)在Castle.Proxies.Invocations.ControllerActionInvoker_InvokeActionMethod.InvokeMethodOnTarget()在Castle.DynamicProxy.AbstractInvocation.Proceed()在Glimpse.Mvc3.Interceptor.InvokeActionMethodInterceptor.Intercept(IInvocation调用)在Castle.DynamicProxy.AbstractInvocationed()
Castle.Proxies.AsyncControllerActionInvokerProxy.InvokeActionMethod(ControllerContext
controllerContext,ActionDescriptor actionDescriptor,IDictionary``2个参数)at
System.Web.Mvc.Async.AsyncControllerActionInvoker。<> c__DisplayClass37。<>
c__DisplayClass39.b__33()at System.Web.Mvc.Async
System.Web.Mvc.Async.AsyncControllerActionInvoker的<>
c__DisplayClass4f.b__49().System.Web.Mvc.Async的<>
c__DisplayClass37.b__36(IAsyncResult
asyncResult)。AsyncControllerActionInvoker。<> c__DisplayClass25。<>
c__DisplayClass2a.b__20()在System.Web.Mvc.Async.AsyncControllerActionInvoker。<>
c__DisplayClass25.b__22(IAsyncResult asyncResult)


阅读 651

收藏
2020-05-19

共1个答案

小编典典

我认为当您在运行时为空引用的lambda中使用变量时,会发生此令人困惑的异常。在您的情况下,我将检查您的变量CalculationViewModel是否为空引用。

就像是:

public ActionResult MNPurchase()
{
    CalculationViewModel calculationViewModel = (CalculationViewModel)TempData["calculationViewModel"];

    if (calculationViewModel != null)
    {
        decimal OP = landTitleUnitOfWork.Sales.Find()
            .Where(x => x.Min >= calculationViewModel.SalesPrice)
            .FirstOrDefault()
            .OP;

        decimal MP = landTitleUnitOfWork.Sales.Find()
            .Where(x => x.Min >= calculationViewModel.MortgageAmount)
            .FirstOrDefault()
            .MP;

        calculationViewModel.LoanAmount = (OP + 100) - MP;
        calculationViewModel.LendersTitleInsurance = (calculationViewModel.LoanAmount + 850);

        return View(calculationViewModel);
    }
    else
    {
        // Do something else...
    }
}
2020-05-19