Friday, 15 April 2011

c# - How to call a child action in the Home controller -



c# - How to call a child action in the Home controller -

this controller , partial view producdetails,when phone call kid action method in default home controller @html.action("displayproduct", product),i cannot work idea? 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", cost = 10m}, new product { id =2, name ="product 2", description ="description 2", cost = 20m}, new product { id =3, name ="product 3", description ="description 3", cost = 30m}, new product { id =4, name ="product 4", description ="description 4", cost = 40m} }; homecoming view(products); } [childactiononly] public actionresult displayproduct(product product) { homecoming partialview("productdetails", product); } }

}

html.action expecting have route values @ sec argument instead of custom model. that's why not receive product object if phone call @html.action("displayproduct", product) immediatly.

if want on it, need send route values instead of object when calling html.action. can utilize sample anonymous object :

@html.action("displayproduct", new { product.id, product.name, product.description, product.price } ) ;

edit :

if you're doing returning "productdetails" view on action, why don't utilize html.renderpartial immediatly instead of using html.action:

@{ html.renderpartial("productdetails", product) ; }

c# asp.net-mvc-4

No comments:

Post a Comment