在一个特定的上下文中缺少AspNetCore ModelState类型

时间:2019-12-02 00:50:45

标签: c# asp.net-core

我在Asp Net Core(2.2)项目中有一个Controller动作。 我已经宣布

    using Microsoft.AspNetCore.Mvc.ModelBinding;

操作代码如下:

    1:   public IActionResult Index()
    2:   {
    3:       var errors = ModelState.Values.SelectMany(v => v.Errors);
    4:       foreach ( ModelState modelState in ViewData.ModelState.Values)
    ...

由于某种原因,编译器在第4行突出显示一条消息

  

错误CS0246找不到类型或名称空间名称'ModelState'(您是否缺少using指令或程序集引用?)ics-billing C:\ Users ... \ Controllers \ HomeController.cs 30有效

我不明白编译器如何无法在第4行上解析类型'ModelState',而在第3行上引用完全相同的类却没有问题?

2 个答案:

答案 0 :(得分:2)

第三行上的

File "Main_File.py", line 76, in <module> model.predict(testfeat) File "G:\Software\Anaconda\lib\site-packages\sklearn\multiclass.py", line 310, in predict indices.extend(np.where(_predict_binary(e, X) > thresh)[0]) File "G:\Software\Anaconda\lib\site-packages\sklearn\multiclass.py", line 98, in _predict_binary score = estimator.predict_proba(X)[:, 1] File "G:\Software\Anaconda\lib\site-packages\sklearn\ensemble\bagging.py", line 698, in predict_proba for i in range(n_jobs)) File "G:\Software\Anaconda\lib\site-packages\joblib\parallel.py", line 1003, in __call__ if self.dispatch_one_batch(iterator): File "G:\Software\Anaconda\lib\site-packages\joblib\parallel.py", line 834, in dispatch_one_batch self._dispatch(tasks) File "G:\Software\Anaconda\lib\site-packages\joblib\parallel.py", line 753, in _dispatch job = self._backend.apply_async(batch, callback=cb) File "G:\Software\Anaconda\lib\site-packages\joblib\_parallel_backends.py", line 201, in apply_async result = ImmediateResult(func) File "G:\Software\Anaconda\lib\site-packages\joblib\_parallel_backends.py", line 582, in __init__ self.results = batch() File "G:\Software\Anaconda\lib\site-packages\joblib\parallel.py", line 256, in __call__ for func, args, kwargs in self.items] File "G:\Software\Anaconda\lib\site-packages\joblib\parallel.py", line 256, in <listcomp> for func, args, kwargs in self.items] File "G:\Software\Anaconda\lib\site-packages\sklearn\ensemble\bagging.py", line 129, in _parallel_predict_proba proba += proba_estimator ValueError: operands could not be broadcast together with shapes (100000,2) (100000,) (100000,2) ModelState类的属性,它是ControllerBase继承的属性。在第四行,它正在寻找一个名为Controller的类型,但是该类型不存在。 ModelState属性的类型为ModelStatehttps://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.modelstate?view=aspnetcore-3.0#Microsoft_AspNetCore_Mvc_ControllerBase_ModelState

ModelStateDictionary的{​​{1}}属性的类型为Valueshttps://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.modelbinding.modelstatedictionary.values?view=aspnetcore-3.0#Microsoft_AspNetCore_Mvc_ModelBinding_ModelStateDictionary_Values

这是ModelStateDictionary中的ValueEnumerablehttps://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.modelbinding.modelstatedictionary.valueenumerable?view=aspnetcore-3.0

我相信您需要将第四行的IEnumerable更改为ModelStateEntry。但是,您也可以使用ModelState modelState代替指定类型名称。

答案 1 :(得分:0)

您可以通过以下代码循环查找ModelState错误:

            if (!ModelState.IsValid)
            {
                ViewBag.Message = string.Join("; ", ModelState.Values
                                    .SelectMany(x => x.Errors)
                                    .Select(x => x.ErrorMessage));                    
                return View(model);
            }