gpt4 book ai didi

javascript - 将数组中的 JSON 列表绑定(bind)到 ASP.NET 模型类

转载 作者:行者123 更新时间:2023-11-28 01:28:05 24 4
gpt4 key购买 nike

我正在从我的数组构建 JSON 输出,我打算将其传递回服务器,在服务器中我有模型类将 JSON 数据变量绑定(bind)到类变量。在这个类中,我还为参数“Component”获取多个记录,并绑定(bind)这部分,我在模型类中有 IList。

现在我已经成功地将数据传递回 Controller ,除了 IList 中的组件...我正在努力寻找答案..非常感谢您的帮助..

模型类

 public class QualificationElementComponents_ViewModel
{

public int ElementIndex { get; set; }
public string ElementMarkingSchemeTitle { get; set; }
public int ElementAvailableMark { get; set; }
public int ElementPassMark { get; set; }
public int ElementMeritMark { get; set; }
public int ElementDistinctionMark { get; set; }

public IList<ECom1> ElementComponent { get; set; }

}

IList“组件”模型类

 public class ECom1
{
public int componentIndex { get; set; }
public int componentMark { get; set; }
}

Controller 方法

  public ActionResult CreateNewQualification(QualificationViewModel newQualificationData, IList<QualificationElementComponents_ViewModel> ElementComponentList)
{

在 View 中

//build component list... possible will have multiple records in array
selectedComponentList.push({ componentIndex: recordId, componentMark: ComponentSchemeMark });

// build element list
selectElementList.push({ ElementIndex: E_RecordId, ElementMarkingSchemeTitle: E_MarkingSchemeTitle, ElementAvailableMark: E_AvailableMark, ElementPassMark: E_PassMark, ElementMeritMark: E_MeritMark, ElementDistinctionMark: E_DistinctionMark });

//bind arrays

selectElementList.push({ ElementComponent: selectedComponentList });

QualificationElemenetsAndComponentsList.push.apply(QualificationElemenetsAndComponentsList, selectElementList);

JSON 输出

{"QualificationElemenetsAndComponentsList":[{"ElementIndex":1,"ElementMarkingSchemeTitle":"fg","ElementAvailableMark":"56","ElementPassMark":"6","ElementMeritMark":"5","ElementDistinctionMark":"6"},{"ElementComponent":[{"componentIndex":1,"componentMark":"23"},{"componentIndex":2,"componentMark":"44"}]}]}

需要 JSON 输出

与上面的 JSON 相比,我需要以下 JSON 格式

{"QualificationElemenetsAndComponentsList":[{"ElementIndex":1,"ElementMarkingSchemeTitle":"d2","ElementAvailableMark":"223","ElementPassMark":"32","ElementMeritMark":"12","ElementDistinctionMark":"2","ElementComponent":[{"componentIndex":2,"componentMark":551}]}]}

最佳答案

您不需要将 ElementComponent 属性添加到新对象,然后添加到数组中,而是需要将其与其他属性一起包含在内,如下所示:

    //build component list... possible will have multiple records in array
selectedComponentList.push({ componentIndex: recordId, componentMark: ComponentSchemeMark });

// build element list
selectElementList.push({ ElementIndex: E_RecordId, ElementMarkingSchemeTitle: E_MarkingSchemeTitle, ElementAvailableMark: E_AvailableMark, ElementPassMark: E_PassMark, ElementMeritMark: E_MeritMark, ElementDistinctionMark: E_DistinctionMark, ElementComponent: selectedComponentList });
//Add ElementComponent with all the other properties

关于javascript - 将数组中的 JSON 列表绑定(bind)到 ASP.NET 模型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22467661/

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