Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8928007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:20:19+00:00 2026-06-15T08:20:19+00:00

I am trying to create Product instance in Javascript and than to pass it

  • 0

I am trying to create Product instance in Javascript and than to pass it to the server using [webmethod].

[WebMethod]
public static void SetProduct(Product product)
{    
     // i want a product instance    
}

Following is Product class that i’m trying to create:

public class Product
{
    public Type Type { get; set; }
    public Foo Foo { get; set; }
    public List<Bar> Bars { get; set; }
}

public class Type
{
    public string ID { get; set; }
}

public class Foo
{
    public string ID { get; set; }
    public string Color { get; set; }
}

public class Bar
{
    public string Name { get; set; }
}

I am being able to create Type and Foo but not List<Bar> in Javascript: (see my comments in the code for more details)

Javascript

function setProduct() {
    var product = {};
    product.Type = {};
    product.Foo = {};

    product.Type.ID = 'typeID';
    product.Foo.ID = 'fooID';
    product.Foo.Color = 'fooColor';

    //here is my question how can create List<Bar> Bars and add it to product item???

    $.ajax({
        type: "POST",
        url: "Default.aspx/SetProduct",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        data: "{product:" + JSON.stringify(product) + "}",
    });
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:20:20+00:00Added an answer on June 15, 2026 at 8:20 am

    JavaScript doesn’t know what a List<T> is. It only knows how to make arrays. So you’ll have to construct an array of Bars and pass that in the JSON.

    Fortunately, it’s an easy fix:

    product.Bars = [
        { Name: "bar 1" },
        { Name: "bar 2" },
        { Name: "bar 3" },
    ];
    

    The above is probably all you need. I’m pretty sure ASP.NET will be smart enough to convert that Bar[] into a List<Bar> automagically, but just in case it isn’t:

    public class Product
    {
        public Type Type { get; set; }
        public Foo Foo { get; set; }
        public IEnumerable<Bar> Bars { get; set; }
    }
    

    Then if you still want List<T> functionality, just convert the array to a List in your WebMethod:

    [WebMethod]
    public static void SetProduct(Product product)
    {    
         var list = product.Bars.ToList();
         product.Bars = list;
         return product;
    }
    

    Now you can still access those nice List<T> methods:

    ((List<Bar>)product).Add(new Bar() { Name = "bar 4" });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new with MVC I am trying to create a small product form
I am trying to create an array of product SKU's which I can then
I'm trying to create a basic shopping cart, having an issue with the product
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Ok so I am trying create a login script, here I am using PHP5
I've been trying to create content in my plone site using the Dexterity tool
I'm trying to create a table of information in a database query using Zend
I am trying to create a custom product page http://www.ebay.co.uk/itm/Que-Modular-Shotgun-Microphone-Kit-/271032405588?pt=UK_Music_Instruments_Microphones_MJ&hash=item3f1aca4e54#ht_2243wt_971 on eBay, but am
I ma trying to create 2 filters for a product list page, one for
I am trying to create an in-app product for my app with a price

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.