2016-06-29 25 views
0

我有一个工作的创建页面,它使用JavasScript来拉动各种数据字符串,并在前端显示它们。MVC 4编辑控制器抛出NullReferenceException当使用.getJSON调用

当我试图在同一视图的编辑页面中实现类似的代码时,我遇到了一个“NullReferenceException” - 对象引用未设置为对象的实例。

下面是在“ProductController的”编辑方法,错误是在IF语句抛出:

public ActionResult Edit(int id = 0) 
    { 
     using (ManagePromotion promLogic = new ManagePromotion(ref uow)) 
     using (ManageDistributionRule druleLogic = new ManageDistributionRule(ref uow)) 
     { 
      Product prd = ProductLogic.GetByAndInclude(x => x.ProductID == id, new List<string>() { "AddSamplePacks" }).FirstOrDefault(); 
      ProductVM vm = new ProductVM(prd, (int)System.Web.HttpContext.Current.Session["CustomerID"]); 

      if (vm.Product.AddSamplePacks == null || promLogic.GetById(vm.Product.PromotionID).CustomerID != (int)System.Web.HttpContext.Current.Session["CustomerID"]) 
      { 
       throw new HttpException(404, ""); 
      } 
      return View(vm); 
     } 

    } 

下面也就是我一直在添加到编辑页面前端的JavaScript,如果我删除此代码的页面运行正常,没有错误:

var input = "comm"; 
    // 
    //alert("HIT"); 
    $.getJSON('getCat', { term: input }, function (result) { 
     var ddl = $('#selectTypeCat'); 
     alert(result); 
     // 
     var length = result.length; 
     ddl.empty(); 

     $(document.createElement('option')) 
       .attr('value', 0) 
       .text("-- Please Select --") 
       .appendTo(ddl); 

     $(document.createElement('option')) 
       .attr('value', 100) 
       .text("All Distribution Rules") 
       .appendTo(ddl); 

     $(result).each(function (jack) { 
      // 
      $(document.createElement('option')) 
       .attr('value', result[jack]) 
       .text(result[jack]) 
       .appendTo(ddl); 
     }); 
    }); 

在产品控制器,它与创建过程中工作得很好,在“getCat”的方法是这样的:

 public ActionResult getCat(string term)                
    {                         
     int customerId = (int)System.Web.HttpContext.Current.Session["CustomerID"];      
     // 
     //string term = "small";                   
     // 
     var getCat = (from ctype in TypeLogic.GetCustomerProdIndex(customerId, term).List    
         select new { ctype.TypeName, ctype.TypeCategoryID }).ToList();      

     List<String> returnList = new List<string>(); 


     foreach (var item in getCat)                  
     {                        

       returnList.Add(item.TypeName.ToString());                               
     }                        

     returnList = returnList.Distinct().ToList();          
     return Json(returnList, JsonRequestBehavior.AllowGet);           
    } 

任何帮助将不胜感激,谢谢。

+0

请出示“getCat”功能是不是在更新控制器 –

+0

,感谢 – Cooper1810

+0

的根本原因是您尝试在vm.Product.AddSamplePacks获得值,其中vm.Product为空(见我的回答) –

回答

0

我终于体会到了什么问题。

在我的JavaScript我有一个JSON调用是这样的:

$.getJSON('getDistributionNameBox', { term: input }, function (result) 

然而,因为它是一个“编辑”页它通过HTTP编辑方法自动跑每当这个请求被调用。

FIX:

我,而不是改变了的Javascript我JSON方法的调用,使其显式调用问题的方法:

$.getJSON('/CustomerArea/Product/getDistributionNameBox', { id: drID }, function (result) 

这意味着编辑方法,不再重新叫并且可以在页面中多次运行动态JSON请求。

0

你检查m.Product.AddSamplePacks == null但你不检查vm.Product是空的。刚刚加入vm.Product == NULL

public ActionResult Edit(int id = 0) 
{ 
    using (ManagePromotion promLogic = new ManagePromotion(ref uow)) 
    using (ManageDistributionRule druleLogic = new ManageDistributionRule(ref uow)) 
    { 
     Product prd = ProductLogic.GetByAndInclude(x => x.ProductID == id, new List<string>() { "AddSamplePacks" }).FirstOrDefault(); 
     ProductVM vm = new ProductVM(prd, (int)System.Web.HttpContext.Current.Session["CustomerID"]); 

     if (vm.Product == null || vm.Product.AddSamplePacks == null || promLogic.GetById(vm.Product.PromotionID).CustomerID != (int)System.Web.HttpContext.Current.Session["CustomerID"]) 
     { 
      throw new HttpException(404, ""); 
     } 
     return View(vm); 
    } 

} 
+0

它也可以''promLogic.GetById(vm.Product.PromotionID).CustomerID'因为'GetById(...)'可能会返回null。 –

+0

我已经添加了vm.product == null,现在httpexception被抛出。 getJSON请求仍然不会发生。 – Cooper1810

+0

我还有另一个问题,你的代码现在不会抛出nullReferenceexception –

相关问题