When I'm in a controller, I need to pass an entity object (Product
) back to a view for use in JavaScript.
I pass a model object from the action method to the view. The model object contains some data the view needs for display, but also (the bit I'm struggling with) a JSON version of the product data.
In the view, I want to pick up the product object as JavaScript to play with.
Controller:
public ActionResult ViewProduct( int productKey )
{
VendorPage page = PageManager.Instance().GetProductPage( );
Product product = this.repoProducts.Get<Product>( App.GetVendorKey(), productKey );
JavaScriptSerializer sz = new JavaScriptSerializer();
string json = sz.Serialize( new { pr = product } );
ProductPageModel ppm = new ProductPageModel( page, product );
// Embed the product as json in the model
ppm.js = json;
if ( product != null )
{
return View( "Product", ppm );
}
return null;
}
View - uses the model as ProductPageModel @model SiteEngine.SiteEngineUI.Models.ProductPageModel html......
So, the question is: How do I gain access to the product in JavaScript, in order to do something like ...
alert( product.Name );