c# - How to call a child action in the Home controller View called index -
i have controller named product , partial view named product details want call child action inside index view of default home controller , tried , model returned null in view.
here product controller , here ids how call child action in view.
@foreach (var product in model) { html.action("displayproduct", new { product.id, product.name, product.description, product.price } ) ; }
and
public class productcontroller : controller { // // get: /product/ public actionresult index() { list<product> products = new list<product>() { new product { id =1, name ="product 1", description ="description 1", price = 10m}, new product { id =2, name ="product 2", description ="description 2", price = 20m}, new product { id =3, name ="product 3", description ="description 3", price = 30m}, new product { id =4, name ="product 4", description ="description 4", price = 40m} }; return view(products); } [childactiononly] public actionresult displayproduct(product product) { return partialview("displayproduct", product); } }
you create anonymous type new { product.id, product.name, product.description, product.price }
- supply product directly:
@foreach (var product in model) { @html.action("displayproduct", product) }
and if child action returns partial not needed @ can call partial directly:
@foreach (var product in model) { @html.partial("displayproduct", product) }
Comments
Post a Comment