1

I am building JSON output from my array that I am intended to pass back to server where I have model class to bind JSON data variable to class variables. In this class I am also taking multiple records of say for argument 'Component' and to bind this part I have IList in my model class.

Now I have managed to pass data back to controller except the Components that is in IList... I am struggling to find answer.. your help will be really appreciated..

Model class

 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 'Component' Model class

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

Controller Method

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

in 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 Output

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

require JSON Output

in comparison to above JSON I require following JSON format

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

1 Answer 1

1

Instead of adding the ElementComponent property to a new object and then into the array, you need to include it with the other properties like so:

    //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
Sign up to request clarification or add additional context in comments.

2 Comments

but my issue is i got to take element before component at different places i don't have the choice
@toxic, You can add the ElementComponent property initially as an empty array like: ElementComponent: [] then later on update the object in the selectedElementList. Or is this not feasible?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.