C# ModelStateDictionary不包含CopyTo的定义
在ASP.NET Core RC 1(完整的.NET Framework)中,以下代码适用于我:C# ModelStateDictionary不包含CopyTo的定义,c#,asp.net-core,asp.net-core-mvc,asp.net-core-1.0,C#,Asp.net Core,Asp.net Core Mvc,Asp.net Core 1.0,在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
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);
}
}
}
}
}
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.AspNet.Mvc;
使用Microsoft.AspNet.Mvc.Filters;
使用Microsoft.AspNet.Mvc.ModelBinding;
使用Newtonsoft.Json;
命名空间MyProject.Classes.Filters.ModelState
{
公共类SetTempDataModelStateAttribute:ActionFilterAttribute
{
公共覆盖无效OnActionExecuted(ActionExecutedContext筛选器上下文)
{
base.OnActionExecuted(filterContext);
var控制器=filterContext.controller作为控制器;
如果(控制器!=null)
{
var modelState=controller.ViewData.modelState;
if(modelState!=null)
{
var dictionary=newkeyvaluepair[modelState.Count];
modelState.CopyTo(字典,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(完整的.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);
}
}
}
}
}
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.AspNetCore.Mvc.Filters;
使用Microsoft.AspNetCore.Mvc.ModelBinding;
使用Newtonsoft.Json;
命名空间MyProject.Models.ModelState
{
公共类SetTempDataModelStateAttribute:ActionFilterAttribute
{
公共覆盖无效OnActionExecuted(ActionExecutedContext筛选器上下文)
{
base.OnActionExecuted(filterContext);
var控制器=filterContext.controller作为控制器;
如果(控制器!=null)
{
var modelState=controller.ViewData.modelState;
if(modelState!=null)
{
var dictionary=newkeyvaluepair[modelState.Count];
modelState.CopyTo(字典,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”不包含“CopyTo”和的定义
没有扩展方法“CopyTo”接受类型为的第一个参数
可以找到“ModelStateDictionary”(您是否缺少使用
指令或组件引用?)
可能我需要将新引用连接到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)
);
这在功能上应该与您在原始代码段中所做的相同。也许添加
System.Web
可以解决此错误。@diiN\uuno,添加System.Web
并没有解决问题。请不要对asp.net核心MVC相关问题使用asp.net-mvc6
标记,以免混淆未来的asp.net MVC版本。也不要将标签塞进问题标题ModelStateDictionary
没有实现IDictionary
,因此没有CopyTo
方法。在您的例子中,可以用var listErrorr=modelState.ToDictionary(m=>m.Key,m=>m.Value.Errors.Select(s=>s.ErrorMessage.FirstOrDefault(s=>s!=null))替换代码代码>,这在功能上应该与您在代码段中所做的相同。@Pranav谢谢,它可以工作。请发表此评论作为答复。