Asp.net mvc 是否有现成的代码可以使用ModelMetaData将模型序列化为键值对?

Asp.net mvc 是否有现成的代码可以使用ModelMetaData将模型序列化为键值对?,asp.net-mvc,modelmetadata,Asp.net Mvc,Modelmetadata,我知道ModelMetadata用于将值从模型绑定到字段。是否有现成的MVC代码将采用模型并使用其ModelMetadata为其所有属性的值生成KeyValuePairs?您可以使用System.Linq中的.ToDictionary扩展方法 Dictionary<string, object> dictionary = ViewData.ModelMetadata.Properties.ToDictionary(key => key.PropertyName, value =

我知道ModelMetadata用于将值从模型绑定到字段。是否有现成的MVC代码将采用模型并使用其ModelMetadata为其所有属性的值生成KeyValuePairs?

您可以使用System.Linq中的.ToDictionary扩展方法

Dictionary<string, object> dictionary = ViewData.ModelMetadata.Properties.ToDictionary(key => key.PropertyName, value => value.Model);

我不会在ViewData可用的代码中调用此代码。我想开始的方法是构造ViewData以到达该点。