-6

I would like to create a variable and populate data and make the output of the variable like the following pseudo code:

myObj = {
    "Title":"Test",
    "Paragraphs": 
    [
        { 
            "Order":"1", 
            "Id": "2" 
        },
        { 
            "Order":"2", 
            "Id": "1"
        },
        { 
            "Order":"3", 
            "Id": "2"
        }
    ]
 }

And use JQuery to POST to MVC controller action at the back end using a defined data model:

public class TemplateBuilderViewModel
{
    public string Title { get; set; }

    // Not sure if I should use Dictionary or something else
    public Dictionary<int, int> Paragraphs { get; set; }
}

I am not sure how to declare the variable and populate data into the variable with the data structure I want in the output.

Any suggestions? Thank you.

Xiao Han
  • 1,004
  • 7
  • 16
  • 33
  • 1
    unclear what you are asking – epascarello Oct 05 '17 at 15:17
  • 1
    Dictionaries don't exist in js. you can create object within objects. – jdmdevdotnet Oct 05 '17 at 15:18
  • 1
    I mean, its called an object, but his concept of a dict is just a map. – Fallenreaper Oct 05 '17 at 15:18
  • What you have is the closest you can get to a dictionary, so what are you asking? – jdmdevdotnet Oct 05 '17 at 15:18
  • @epascarello It is very clear what he is asking. He wants to create an object, in javascript, that has that form. He is trying to simulate a dictionary in Javascript... – Maderas Oct 05 '17 at 15:19
  • @Fallenreaper my guess is he comes from a C#/Java background where dictionaries exist with data types. Which I believe is what he's asking how to do in js? Not sure, he needs to clarify, but if that's the case then my comment stands. Dictionaries don't exist in the way they do in C# or Java. – jdmdevdotnet Oct 05 '17 at 15:20
  • @Maderas No it is unclear where the data is coming from to populate it. We can make assumptions, but assumptions leads to errors. And with 4 downvotes, it appears to be unclear. – epascarello Oct 05 '17 at 15:20
  • I think we're all assuming our own bias' here. The OP needs to explicitly ask what he wants to ask. What he has is valid, so not sure if he's asking if it's valid what he has or if he does it another way? – jdmdevdotnet Oct 05 '17 at 15:21
  • Thanks for your replies. I am trying to pass a key/value collection using JQuery to backend. – Xiao Han Oct 05 '17 at 15:23
  • 1
    Going by the data format you provided. I think you are looking for JSON. – Master Yoda Oct 05 '17 at 15:31
  • @Master Yoda, yes, I am creating an object which one of the property will be set to this Dictionary like variable in JQuery and pass to MVC Controller action to map to ViewModel. – Xiao Han Oct 05 '17 at 15:52
  • @jdmdevdotnet, yes I notice Dictionary doesn't not exist in JavaScript, and it would be either Array or object. So I guess create an object would be my choice. – Xiao Han Oct 05 '17 at 16:17
  • Guys, what I am trying to do is passing a collection of data in Key value pair style to back end from the page, bind to MVC ViewModel so the controller action would parse into the ViewModel and I can read the data of the collection in the action method. – Xiao Han Oct 05 '17 at 16:19

1 Answers1

0

This is what I get working in the end in JQuery. Declare an array type, use push to insert values and assgin the arrary variable into the object type variable.

var dict = [];


for (i = 1; i < counter; i++) {
    var paragraphId = Id;
    var order = order;

    dict.push({
        "ParagraphId": paragraphId,
        "ParagraphOrder": order
    });
}

var templateBuilder = {
    TemplateTitle: title,
    ParagraphTitle: "",
    ParagraphTitleList: undefined,
    ParagraphText: "",
    ParagraphOrder: dict
};
Xiao Han
  • 1,004
  • 7
  • 16
  • 33