对象引用未设置为对象的实例#7

时间:2018-04-11 03:35:13

标签: asp.net-mvc entity-framework-6

当我尝试在服务器上部署我的项目时,我开始得到跟随错误。这对我来说是莫名其妙的行为,因为我在本地计算机和服务器之间做了类似的操作。

  

[NullReferenceException:对象引用未设置为对象的实例。]          MyProject.Models.ViewModels.CashboxViewModel..ctor(Cashbox entity)+168          MyProject.Controllers.CashboxController.Index(Nullable 1 id) +194 lambda_method(Closure , ControllerBase , Object[] ) +150 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary 2个参数)+241          System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary 2 parameters) +38 System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +11 System.Web.Mvc.Async.WrappedAsyncResult 2.CallEndDelegate(IAsyncResult asyncResult)+138          System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3d()+111          System.Web.Mvc.Async。&lt;&gt; c__DisplayClass46.b__3f()+ 452          System.Web.Mvc.Async。&lt;&gt; c__DisplayClass33.b__32(IAsyncResult asyncResult)+15          System.Web.Mvc.Async。&lt;&gt; c__DisplayClass2b.b__1c()+37          System.Web.Mvc.Async。&lt;&gt; c__DisplayClass21.b__1e(IAsyncResult asyncResult)+241          System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult,ExecuteCoreState innerState)+29          System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +111 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53 System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult)+19          System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult,ProcessRequestState innerState)+51          System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)+111          System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+606          System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+288

控制器

[HttpGet]
public ActionResult Index(Guid? id)
{
    Cashbox obj = null;
    if (id != null)
    {
        obj = service.GetById(id.Value);
    }

    var model = (TempData[SESSION_KEYS.Transfer] as CashboxViewModel) ?? new CashboxViewModel(obj);
    SetReferenceList(model);

    return View(model);
}

    public BaseViewModel(TEntity obj)
    {
        Set(obj);
    }

    protected void Set(TEntity obj, params string[] constrains)
    {
        if (obj != null)
        {
            foreach (var info in obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(x => x.CanRead))
            {
                if (constrains.Contains(info.Name))
                {
                    continue;
                }

                var piDest = this.GetType().GetProperty(info.Name);
                if (piDest != null && piDest.CanWrite)
                {
                    piDest.SetValue(this, info.GetValue(obj));
                }
            }
        }
    }

public class CashboxViewModel: BaseViewModel<Cashbox>
{

    public CashboxViewModel()
        : base()
    {

    }

    public CashboxViewModel(Cashbox entity)
        : base(entity)
    {

    }
}

0 个答案:

没有答案
相关问题