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
.
精彩评论