开发者

passing collection from constructor to controller action method

开发者 https://www.devze.com 2023-02-26 05:57 出处:网络
I want to pass a list of products ( which is being created in controller\'s constructor) to action method of controller. How can I pass it to controller action and then further to view ?

I want to pass a list of products ( which is being created in controller's constructor) to action method of controller. How can I pass it to controller action and then further to view ?

I am doing like this:

 public ProductsController()
        {
            var products = ne开发者_如何学Pythonw List<Product> {
                new Product {Id=1,Name="Milk",Price=1.99m},
                new Product {Id=1,Name="Bread",Price=3.00m},
                new Product {Id=1,Name="Steak",Price=12.00m}
            };
        }

How can I access this collection in action method ?

Regards, Asif Hameed


For Constructor -> Action Method:

Declare an instance field or property on your controller class and assign the list to it. Then every action method in the controller will be able to access it.

For Action Method -> View:

Either create a strongly-typed model class that has a field that can store your collection or simply pass it into the view using ViewData/ViewBag.

0

精彩评论

暂无评论...
验证码 换一张
取 消