Javascript JSON或数组的MVC4 ModelState属性名称
我正在使用一个复杂的对象图,它序列化为JSON,并带有MVC4/jQuery/Sammy/Rivets的SPA功能 我有一个对象图,当序列化为JSON(显然是模拟的)时,它看起来有点像这样: 在我需要不引人注意的验证之前,一切都运行得很好,因为我的爱好在一个光滑的网格中,我自己管理所有的数据。为了处理这个问题,我将在模型旁边返回带有JSON的ModelStateJavascript JSON或数组的MVC4 ModelState属性名称,javascript,arrays,asp.net-mvc-4,Javascript,Arrays,Asp.net Mvc 4,我正在使用一个复杂的对象图,它序列化为JSON,并带有MVC4/jQuery/Sammy/Rivets的SPA功能 我有一个对象图,当序列化为JSON(显然是模拟的)时,它看起来有点像这样: 在我需要不引人注意的验证之前,一切都运行得很好,因为我的爱好在一个光滑的网格中,我自己管理所有的数据。为了处理这个问题,我将在模型旁边返回带有JSON的ModelState return JSON(new { model = model, modelState = this.ModelState });
return JSON(new { model = model, modelState = this.ModelState });
从这里开始,我打算遍历modelState,并使用一些自定义函数将错误分配到正确的位置,但有一个问题
ModelState如下所示:
"Name",
"Age",
"Hobbies[0].Name",
"Hobbies[0].IsActive",
"Hobbies[1].Name",
"Hobbies[1].IsActive"
我需要将[0]分为一个对象,[1]分为它们自己的对象,这样才能顺利地获得值。当我开始考虑复杂对象数组的第三层时,这让我感到困惑
解决方案:
var ModelStateConverter = function ($, module) {
module = module || {};
// Convert The ModelState form style object to a standard JS object structure.
module.toObject = function (modelState) {
var ModelState = {};
$.each(modelState, function (key, value) {
AssignValuesToObjectStore(key, ModelState, value);
});
return ModelState;
}
// item is the full identifier ex. "Hobbies[0].Name"
// store is the object we are going to throw arrays, objects, and values into.
// value is the error message we want to get in the right place.
// index is an internal processing parameter for arrays only, setting it's value has no effect.
function AssignValuesToObjectStore(item, store, value, index) {
var periodMatch = item.match(/[\.]/);
if (periodMatch === null) {
if (Array.isArray(store)) {
if (store[index] === undefined) {
store[index] = {};
}
store[index][item] = value;
}
else {
store[item] = value;
}
}
else {
// This wasn't a simple property or end of chain.
var currentProperty = item.slice(0, periodMatch.index); // Get our property name up to the first period.
var container = {}; // We assume we are dealing with an object unless proven to be an array.
var arrayIndex; // This is irrelevant unless we have an array.
if (currentProperty.slice(-1, currentProperty.length) === "]") {
// We are dealing with an array! Hoo Ray?!
arrayIndex = parseInt(currentProperty.slice(currentProperty.indexOf("[") + 1, currentProperty.indexOf("]")));
currentProperty = currentProperty.slice(0, currentProperty.indexOf("[")); // remove the indexer ex. [0] so we are left with the real name
container = []; // We know we need an array instead;
}
if (store[currentProperty] === undefined) {
store[currentProperty] = container; // If this property isn't already created, then do so now.
}
//Recurseive nature here.
AssignValuesToObjectStore(item.slice(periodMatch.index + 1, item.length), store[currentProperty], value, arrayIndex);
}
}
return module;
}($, ModelStateConverter);
您可以从以下位置调用此选项:
ModelStateConverter.toObject(data.modelState);
其中data.modelState被假定为来自服务器的modelState。您可以尝试使用类库或类来序列化modelState。模型应该是对象的集合还是仅一个对象?在我的情况下,模型始终是一个对象。不要硬编码范围。。如果您有10个以上的元素,则
currentProperty.slice(0,-3)
将失败,因为它将成为cabiods[10]
。@GabyakaG.Petrioli这里有一个很好的例子。
ModelStateConverter.toObject(data.modelState);