首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >绑定到ASP.NET核中的表单编码字典

绑定到ASP.NET核中的表单编码字典
EN

Stack Overflow用户
提问于 2022-07-12 18:34:07
回答 1查看 230关注 0票数 0

我们在ASP.NET MVC 5中运行了一段运行良好的代码,但是一旦升级到ASP.NET核心,它就停止了工作。公布的数据如下:

端点(在ASP.NET核心中):

代码语言:javascript
运行
复制
public IActionResult GetExpressionEditor([FromForm]GetExpressionEditorWidgetModel model)

这些是C#模型类:

代码语言:javascript
运行
复制
public class GetExpressionEditorWidgetModel
{
    public string Name { get; set; }
    public ExpressionEditorViewModel ExpressionEditorData { get; set; }
}

public class ExpressionEditorViewModel
{
    public string Expression { get; set; }

    public IDictionary<string, ExpressionEditorAttribute> SymbolToAttributeMap
    {
        get
        {
            if (this._symbolToAttributeMap == null)
            {
                return new Dictionary<string, ExpressionEditorAttribute>();
            }
            
            return this._symbolToAttributeMap.ToDictionary(entry => entry.Key, entry => ExpressionEditorAttribute.SetDefaults(entry.Key, entry.Value));
        }

        // Default setter
        set { this._symbolToAttributeMap = value; }
    }

    public Dictionary<string, string> Data { get; set; }

    private IDictionary<string, ExpressionEditorAttribute> _symbolToAttributeMap;
}

我在模型绑定中遇到的异常如下:

System.ArgumentNullException

错误消息:值不能为空。参数名称:键

(在System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument论点)

在System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1源代码,Func2 keySelector, Func2 elementSelector,IEqualityComparer1 comparer) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 source,Func2 keySelector, Func2 elementSelector)

在Microsoft.AspNetCore.Mvc.ModelBinding.Binders.CollectionModelBinder1.<BindModelAsync>d__14.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.DictionaryModelBinder2.d__7.MoveNext()

--从抛出异常的前一个位置开始的堆栈跟踪结束--

在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

在Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.d__10.MoveNext()

--从抛出异常的前一个位置开始的堆栈跟踪结束--

在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

在Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.d__10.MoveNext()

--从抛出异常的前一个位置开始的堆栈跟踪结束--

在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

(在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task任务)

在Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.d__11.MoveNext()

经过数小时的调试并试图逐步了解模型绑定的内部,我的结论是ASP.NET Core不喜欢expressionEditorData.data.index形式的“数据”字典的语法,但更喜欢这种语法:

代码语言:javascript
运行
复制
expressionEditorData.data[0].Key = "index"    
expressionEditorData.data[0].Value = 0 

是否有一种使用.key = value绑定字典的方法?还有什么我可以用的模型粘合剂吗?或者为了保持这种功能--定制模型绑定是唯一的方法吗?

EN

回答 1

Stack Overflow用户

发布于 2022-07-12 22:41:02

您必须升级到net6。Net6有一个问题-默认情况下,您的所有属性都应该显式地标记为可空,或者另一种方法是您可以从项目文件中删除一个可空选项。

代码语言:javascript
运行
复制
<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <!--<Nullable>enable</Nullable>-->
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

或将这一行添加到配置中(启动或程序)

代码语言:javascript
运行
复制
services.AddControllers(
options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72956921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档