首页 > 解决方案 > 字符串引用未设置为字符串的实例。参数名称:输入 ASP.Net MVC

问题描述

为什么我看到此错误 prnt.sc/11btyns 。仅当我将 mostRecentMonday 更改为 lastWeekMonday 并将 weekEnd 更改为 lastWeekSunday 时才会发生这种情况。

var mostRecentMonday = DateTime.Now.AddDays(-7).StartOfWeek(DayOfWeek.Monday); 
var weekEnd = mostRecentMonday.AddDays(7).AddSeconds(-1); 
var lastWeekMonday = mostRecentMonday.AddDays(-7).StartOfWeek(DayOfWeek.Monday); 
var lastWeekSunday = lastWeekMonday.AddDays(7).AddSeconds(-1); 

模型类

public DateTime? FeedbackDateTime { get; set; }

public DateTime? FeedbackSharedDateTime { get; set; }

public string AuditorAHT { get; set; }

ReportVM 对数据进行分组并在视图中显示

public string FeedbackSharedBy { get; set; }
public int AuditCount { get; set; }

public string AudtAht { get; set; }

将审计员执行的操作保存为持续时间的控制器

public string AuditorAHT { get; set; }
dto.FeedbackSharedDateTime = DateTime.Now;

string ahtString = string.Format("{0:hh\\:mm\\:ss}", dto.FeedbackSharedDateTime - dto.FeedbackDateTime);

dto.AuditorAHT = ahtString;

db.SaveChanges();

操作下方应显示审核员姓名、计数和平均花费时间。

var audtName = db.Chats.Where(x => System.Data.Entity.DbFunctions.TruncateTime(x.MSTChatCreatedDateTime) >= mostRecentMonday
                           && System.Data.Entity.DbFunctions.TruncateTime(x.MSTChatCreatedDateTime) <= weekEnd && x.Feedback != null && x.FeedbackSharedBy != null).Select(x => new {
x.FeedbackSharedBy,
x.AuditorAHT
}).ToList() // this hits the database
                            
// We need to do grouping in the code (rather than the db)
// because timespans are stored as strings
.GroupBy(e => e.FeedbackSharedBy)
.Select(g => new ReportVM
{
FeedbackSharedBy = g.Key,
AuditCount = g.Count(),
AudtAht = TimeSpan.FromSeconds(g.Sum(t => TimeSpan.Parse(t.AuditorAHT).TotalSeconds / g.Count())).ToString()
})
.OrderByDescending(s => s.AuditCount).ToList();

ViewBag.AudtReport = audtName;

日期时间分机

public static class DateTimeExtensions
    {

        public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
        {
            int diff = (7 + (dt.DayOfWeek - startOfWeek)) % 7;
            return dt.AddDays(-1 * diff).Date;
        }

        static GregorianCalendar _gc = new GregorianCalendar();
        public static int GetWeekOfMonth(this DateTime time)
        {
            DateTime first = new DateTime(time.Year, time.Month, 1);
            return time.GetWeekOfYear() - first.GetWeekOfYear() + 1;
        }

        static int GetWeekOfYear(this DateTime time)
        {
            return _gc.GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
        }
        static DateTime ToCleanDateTime(this DateTime dt)
        {
            return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, 0);
        }

        public static DateTime ToCleanDateTime(this DateTime? dt)
        {
            if (dt.HasValue)
            {
                return dt.Value.ToCleanDateTime();
            }
            return DateTime.Now; // if dt doesn't have value, return current DateTime.
        }


    }

请建议

标签: c#asp.net-mvc

解决方案


只需进行 2 处更改即可解决此问题

  1. 以下属性 public string AuditorAHT { get; 放; } = "00:00:00";. 已更新,因此默认 NULL 值与 00:00:00 一起存储,而不是说 NULL。

  2. 所有过去的数据都必须更新为 00:00:00,因此运行 SQL 查询

Update Chats
Set AuditorAHT = '00:00:00'
Where AuditorAHT IS NULL;

构建项目,wallaa 代码开始响应。

谢谢!


推荐阅读