未将Datatables对象引用设置为一个变量的对象实例

时间:2018-10-30 20:36:04

标签: asp.net asp.net-mvc asp.net-core datatables

这是我的数据表服务器端实现。 FilterInput包含5个变量:

  1. 级别<-字符串
  2. 消息<-字符串
  3. 异常<-字符串
  4. StartDate <-DateTime
  5. EndDate <-DateTime

由于某些原因,当我按原样运行此代码时,总是会收到此错误:

  

{System.NullReferenceException:对象引用未设置为   对象的实例。

这是指这一行:

data = data.Where(
                        u => u.Level.ToString().ToLower().Contains(FilterInput.Level.ToLower()) &&
                        u.Message.ToString().ToLower().Contains(FilterInput.Message.ToLower()) &&
                        u.Exception.ToString().ToLower().Contains(FilterInput.Exception.ToLower())
                    ).ToList();

但是,如果我删除对FilterInput.Exception的搜索,那么一切都会再次正常运行。我已经用输入(“ abc”)或没有输入(“”)对其进行了测试,结果是相同的。其他FilterInputs没有相同的错误。

public JsonResult Search(SearchViewModels Input, EventLogsSearchViewModel FilterInput)
{
    JsonResult result = new JsonResult(null);
    try
    {

        var data = dbContext.EventLogs.ToList();
        int totalRecords = data.Count;

        var modelStructure = new Dictionary<int, string>();
        modelStructure.Add(1, "Level");
        modelStructure.Add(2, "TimeStamp");
        modelStructure.Add(3, "LogEvent");
        modelStructure.Add(4, "Message");
        modelStructure.Add(5, "MessageTemplate");
        modelStructure.Add(6, "Exception");
        modelStructure.Add(7, "Properties");
        var StartDate = FilterInput.StartDate != default(DateTime);
        var EndDate = FilterInput.EndDate != default(DateTime);
        if ((!string.IsNullOrEmpty(FilterInput.Level) && !string.IsNullOrWhiteSpace(FilterInput.Level)) ||
            (!string.IsNullOrEmpty(FilterInput.Message) && !string.IsNullOrWhiteSpace(FilterInput.Message)) ||
            (!string.IsNullOrEmpty(FilterInput.Exception) && !string.IsNullOrWhiteSpace(FilterInput.Exception)) ||
            (StartDate && EndDate))
        {
            data = data.Where(
                u => u.Level.ToString().ToLower().Contains(FilterInput.Level.ToLower()) &&
                u.Message.ToString().ToLower().Contains(FilterInput.Message.ToLower()) &&
                u.Exception.ToString().ToLower().Contains(FilterInput.Exception.ToLower())
            ).ToList();

            data = data.Where(u => u.TimeStamp >= FilterInput.StartDate && u.TimeStamp <= FilterInput.EndDate).ToList();
        }
        if (!(string.IsNullOrEmpty(Input.Order) && string.IsNullOrEmpty(Input.OrderDir)))
        {
            var columnName = modelStructure.FirstOrDefault(f => f.Key == Convert.ToInt32(Input.Order));
            data = data.AsQueryable().OrderBy(columnName.Value + " " + Input.OrderDir).ToList();
        }

        int recFilter = data.Count;
        data = data.Skip(Input.StartRec).Take(Input.PageSize).ToList();
        var modifiedData = data.Select(u => new EventLogsListViewModel
        {
            Id = u.Id,
            Message = u.Message,
            MessageTemplate = u.MessageTemplate,
            Level = u.Level,
            TimeStamp = u.TimeStamp,
            Exception = u.Exception,
            Properties = u.Properties,
            LogEvent = u.LogEvent
        });
        result = this.Json(new
        {
            draw = Convert.ToInt32(Input.Draw),
            recordsTotal = totalRecords,
            recordsFiltered = recFilter,
            data = modifiedData,
            order = Input.Order,
            orderdir = Input.OrderDir
        });
    }
    catch (Exception e)
    {
        logger.LogError(e, LoggingGlobals.LoadingException);
    }
    return result;
}

编辑:即使FilterInput.Exception不为null,该异常仍然会发生

0 个答案:

没有答案