0

I am creating a test project where i have products, i didn't won't to use a database at the beginning so i made Mocks Data that returns a list of products, but i want a user should be able to add to the list.

This is the Model

namespace StoreTest.Models
{
    public class Products
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
    }
}

This is the Mock data class

using System.Collections.Generic;
using StoreTest.Models;

namespace StoreTest.Data.Mocs
{

    public class ProductMocks
    {
        public IList<Products> ProductList
        {
            get
            {
                return
                    new List<Products>
                    {
                        new Products
                        {
                            Id = 1,
                            Name = "Some Data",
                            Price = 34.00
                        },
                        new Products
                        {
                            Id = 2,
                            Name = "More Data",
                            Price = 28.00
                        }
                    };
            }

        }

    }
}

This is the part from the Controller where i want to add to the ProductList

        [HttpPost]
        public IActionResult NewProduct(NewProductViewModel vm)
        {

            if (ModelState.IsValid)
            {
                Products product = new Products()
                {
                    Id = vm.Id,
                    Name = vm.Name,
                    Price = vm.Price
                };

                ProductMocks addProduct = new ProductMocks();
                // THIS IS NOT WORKING 
                productMocs.ProductList.Add(product);
                return View("Index", addProduct);
            }
            else
            {
                return View(vm);
            }     
        }

I am very new to asp.net.

Idon't know if i need to change the whole ProductMocks class, or i only need to add something in the controller?

Thank you in advanced!

6
  • 1
    what you mean by not working, any compile or runtime errors? Commented Feb 7, 2018 at 19:37
  • 2
    In your ProductMocks, your list getter is recreating a new list every time. Make a different method like "Initialize" that adds those 2 items and call it when needed so other code can add to it as well. Commented Feb 7, 2018 at 19:38
  • No i don't have any error, but still the product is not getting passed in to the productList. Commented Feb 7, 2018 at 19:43
  • good catch @SteveGreene Commented Feb 7, 2018 at 19:44
  • @SteveGreene Can you explain more how the code should look like!? Commented Feb 7, 2018 at 19:47

1 Answer 1

2

Your ProductMocks implementation is not correct, as when you access the ProductList property every time get is called and as a result a new object is created so the object in which you add is lost in the memory somewhere.

Change your class to be :

public class ProductMocks
{

    public ProductMocks()
    {

      ProductList = new List<Products>
                   {
                    new Products
                    {
                        Id = 1,
                        Name = "Some Data",
                        Price = 34.00
                    },
                    new Products
                    {
                        Id = 2,
                        Name = "More Data",
                        Price = 28.00
                    }
                };
    }
    public IList<Products> ProductList
    {
        get;
        set;

    }

}

Now setting the values in constructor that are needed at start, and now you can add as needed on the instance you created in Controller.

or another way would be to have a backing field yourself and check it, if it is null then create instance with default values else return the instance as it is without creating another new one :

public class ProductMocks
{
    private IList<Products> _ProductList;
    public IList<Products> ProductList
    {
        get
        {
            if(_ProductList == null)
            {
               _ProductList  = new List<Products>
                {
                    new Products
                    {
                        Id = 1,
                        Name = "Some Data",
                        Price = 34.00
                    },
                    new Products
                    {
                        Id = 2,
                        Name = "More Data",
                        Price = 28.00
                    }
                };
             }

             return _ProductList;
        }

    }

}

But the second approach might result in unexpected behaviors for the callers, as in cases where they would expect ProductList to be null will have two items in it and not null.

Hope it Helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! Was stuck on that for a while

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.