ModelStateDictionary 不包含 CopyTo 的定义

ModelStateDictionary does not contain a definition for CopyTo

在 ASP.NET Core RC 1(完整的 .NET Framework)中,以下代码适用于我:

using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Filters;
using Microsoft.AspNet.Mvc.ModelBinding;
using Newtonsoft.Json;

namespace MyProject.Classes.Filters.ModelState
{
    public class SetTempDataModelStateAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);

            var controller = filterContext.Controller as Controller;
            if (controller != null)
            {
                var modelState = controller.ViewData.ModelState;
                if (modelState != null)
                {
                    var dictionary = new KeyValuePair<string, ModelStateEntry>[modelState.Count];
                    modelState.CopyTo(dictionary, 0);
                    var listError = dictionary.ToDictionary(m => m.Key, m => m.Value.Errors.Select(s => s.ErrorMessage).FirstOrDefault(s => s != null));
                    controller.TempData["ModelState"] = JsonConvert.SerializeObject(listError);
                }
            }
        }
    }
}

但是在ASP.NET Core 1.0 (full .NET Framework) 中,出现错误:

using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Newtonsoft.Json;

namespace MyProject.Models.ModelState
{
    public class SetTempDataModelStateAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);

            var controller = filterContext.Controller as Controller;
            if (controller != null)
            {
                var modelState = controller.ViewData.ModelState;
                if (modelState != null)
                {
                    var dictionary = new KeyValuePair<string, ModelStateEntry>[modelState.Count];
                    modelState.CopyTo(dictionary, 0);
                    modelState = dictionary.[0];
                    var listError = dictionary.ToDictionary(m => m.Key, m => m.Value.Errors.Select(s => s.ErrorMessage).FirstOrDefault(s => s != null));
                    controller.TempData["ModelState"] = JsonConvert.SerializeObject(listError);
                }
            }
        }
    }
}

'ModelStateDictionary' does not contain a definition for 'CopyTo' and no extension method 'CopyTo' accepting a first argument of type 'ModelStateDictionary' could be found (are you missing a using directive or an assembly reference?)

也许我需要将新引用连接到 ASP.NET Core RC 1 中不需要的程序集?

ModelStateDictionary 没有实现 IDictionary<,> 因此没有 CopyTo 方法。在您的情况下,您可以将代码替换为

var listErrorr = modelState.ToDictionary(
  m => m.Key, 
  m => m.Value.Errors
    .Select(s => s.ErrorMessage)
    .FirstOrDefault(s => s != null)
); 

这在功能上应该与您在原始代码段中所做的相同。