gpt4 book ai didi

javascript - MVC4 ModelState 属性名称到 JSON 或 Array

转载 作者:搜寻专家 更新时间:2023-11-01 04:41:45 28 4
gpt4 key购买 nike

我正在使用序列化为 JSON 的复杂对象图,通过 MVC4/jQuery/Sammy/Rivets 实现 SPA 功能。

我有一个对象图,在序列化为 JSON 时看起来有点像这样(显然是模型):

model = 
{
Name: "Me",
Age: 22,
Hobbies:
[
{ Name: "Biking", IsActive: true },
{ Name: "Programming", IsActive: true }
]
}

在我需要非侵入式验证之前,一切都运行良好,因为我的爱好在 SlickGrid 中,并且我自己管理所有数据。为了处理这个问题,我在模型旁边返回了我的 ModelState 和我的 JSON。

return JSON(new { model = model, modelState = this.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。

最佳答案

你可以试试像 JSON.NET 这样的库, 或类 JavaScriptSerializer , 序列化 ModelState。

关于javascript - MVC4 ModelState 属性名称到 JSON 或 Array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20847330/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com