1
定义在ASP.NET核心RC 1(完整的.NET框架)的作品对我下面的代码:ModelStateDictionary不包含CopyTo从
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 1.0的核心(完整的.NET框架),发生错误:
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”不包含一个定义为“CopyTo从”和 没有扩展方法“CopyTo从”接受 类型的第一参数“ModelStateDictionary”可以b Ë(是否缺少using 指令或程序集引用?)
也许我需要一个新的参考连接到未在ASP.NET核心RC 1需要的组件?
也许添加'System.Web'可以解决错误。 –
@diiN_不,添加'System.Web'没有解决问题 –
请不要使用ASP.NET MVC相关问题的'asp.net-mvc6'标签来避免混淆未来的ASP.NET MVC版本。也不要将标签填入问题标题 – Tseng