reference function from cshtml page

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
Summary
I am trying to do this:
var product = _productService.GetProductById(productId);

within the
ProductTemplate.VariantsInGrid.cshtml file;

Where it works
I see it works in the CatalogController.cs because of these 2 lines
using Nop.Web.Models.Catalog;
private readonly IProductService _productService;



What I'm asking
I'm able to add the "using Nop.Web.Models.Catalog;" just fine, but how do I either
add "private readonly IProductService _productService;" to the file
or
populate a variable using the "GetProductByID" function within a cshtml file.


I'm certain I've done this before, it just hasn't been a good programming day for me.
12 years ago
try


@{
    var _productService = EngineContext.Current.Resolve<Nop.Services.Catalog.IProductService>();
}
12 years ago
New York wrote:
try


@{
    var _productService = EngineContext.Current.Resolve<Nop.Services.Catalog.IProductService>();
}


I wish I could give you more than just +1, thankyou soooo much!

Worked perfectly.
12 years ago
That will work but you shouldn't do it.  You're breaking the Model View Controller structure.  The View shouldn't be accessing the service layer and retrieving data.  It should only be formatting the data that's in the model.  If you're missing an object or property, the Model should be modified and the Controller supplies it.

The ProductTemplate.VariantsInGrid view is already using the ProductModel so if something from the Product entity is missing it should be pretty easier to add it in from the Controller when the other stuff like Name, Description, etc is populated.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.