-2

In my scenario i need to store some values in arrays of c# object and set some values to those. Then i need to store those values to database through ajax call. Please help me Any suggestions. I have around three objects available all need to go to database.

There will be form kind of area where user will be adding controls like textbox, textarea,fieldset, section etc., by drag and drop functionality. For each of this elements the name of textbox for eg., will be allowed to do styling like bold, italic, underline etc., there will be a done button on click of this i need to save all set properties of those elements in some javascript array and like this all elements details stored in separate javascript objects and these objects need to store in a array of objects so that i can use that array of objects to save it in a database. This function will be raised on click of done button which is a custom style control button.

$("#btnDoneMini").click(function () {

    var mydata = new Object();
    mydata.id = id;
    mydata.Name = $('#' + id).text();
    $("#tblStyleControlMini").hide();
});

Now i need to store the mydata object into an array .... Please suggest !!

Chethan
  • 99
  • 1
  • 5
  • 13
  • 2
    Looks like you put 0 effort in this problem. Google, try something, come here if it wont work and post what you tried – Milan Halada Apr 24 '14 at 13:21
  • 1
    Simply downvoting a question doesn't make sense. I have been googling to achieve the same. I didnt found any useful information for array of objects. If you dont know answer please leave people out there will answer. u dont have to worry @Uriel_SVK – Chethan Apr 25 '14 at 05:22

2 Answers2

3

So, I reread the question, after the update and it appears you need to do two things:

  1. Push an object that stores various information based on what the user created into a client side array
  2. Push that array to the web server so that you can then save it to a database.

As for the Client side, this is fairly simple:
First declare some object you want to create:

function userCreatedItemObject(id,name)
{
  this.Name = name;
  this.Id = id;
}


Then create some array to store these objects in:

var arrayOfObjects = [];


Then in your click handler, push a new object into the array:

$("#btnDoneMini").click(function () {
 //Instead of this
 //var mydata = new Object();
 //mydata.id = id;
 //mydata.Name = $('#' + id).text();

 //Do This:
 var name = $('#' + id).text();
 arrayOfObjects.push(new userCreatedItemObject(id,name));
 $("#tblStyleControlMini").hide();
});


For the send part, you can then pass this array via ajax an action on a controller. As long as that action takes in a list of objects that have the id and name property, it should pass it through, no problem. Here is an example of that: Passing a list of object via ajax to controller


Here is my previous answer text for reference: This is a similar question on stackoverflow, check this out and see if it resolves your problem: How to Post an array of complex objects with Jquery, Json, and MVC

Community
  • 1
  • 1
JasonWilczak
  • 2,303
  • 2
  • 21
  • 37
  • This should be a comment – gitsitgo Apr 24 '14 at 14:38
  • I can't comment on other people's posts since I don't have enough to do it. However, if that is the answer... – JasonWilczak Apr 24 '14 at 23:45
  • The reason why I said it should be a comment is because 1) this question is beyond vague and OP has attempted nothing, ie. unanswerable, and 2) link only answers aren't considered good answers here, especially without explanations. If the link is broken, your answer becomes useless. Hope you don't get me wrong, I'm not trying to put down your effort to help, but bad questions like these aren't worth your time and aren't good for this site. – gitsitgo Apr 25 '14 at 04:29
  • No worries, I appreciate all feedback, good or bad :) Anyway, I've updated my answer, which I think addresses his/her issue. – JasonWilczak Apr 25 '14 at 18:17
  • Thanks Jason this is what i wanted :) @JasonWilczak – Chethan Apr 28 '14 at 07:29
1

Now after edit makes your question some sense, I think you are trying to do something like this:

var dataarray = $.map(mydata, function(value, index) {
    return [value];
});

Btw it can be still easily googled(Converting a JS object to an array)

Community
  • 1
  • 1
Milan Halada
  • 1,943
  • 18
  • 28