asp.net MVC 如何将数据对象添加到 IList 方法

asp.net MVC How to add a Data object to IList method

我正在创建一个我有产品的测试项目,一开始我不会不使用数据库,所以我制作了 returns 产品列表的模拟数据,但我想要一个用户应该能够添加到列表中。

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);
            }     
        }

我对 asp.net 很陌生。

我不知道我是否需要更改整个 ProductMocks class,或者我只需要在控制器中添加一些东西?

提前致谢!

您的 ProductMocks 实现不正确,因为每次调用 get 时您访问 ProductList 属性 并因此创建一个新对象您添加的对象丢失在内存中的某处。

将您的 class 更改为:

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;

    }

}

现在在构造函数中设置启动时需要的值,现在您可以根据需要在您在控制器中创建的实例上添加。

或另一种方法是自己拥有一个支持字段并检查它,如果它是 null 然后使用默认值创建实例,否则 return 实例,而不创建另一个新实例:

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;
        }

    }

}

但是第二种方法可能会导致调用者出现意想不到的行为,因为在他们期望 ProductListnull 的情况下,其中会有两个项目而不是 null。

希望对您有所帮助。