How can I get the product URL in my view?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Here's what I did which is still not working.  

First, I added a list to the model:

public System.Collections.Generic.List<Nop.Core.Domain.Catalog.Product> ProductList { get; set; }

Then I add items to that list in the controller:

foreach (var item in _orderService.GetOrderById(nopOrderId).OrderItems)
{
    ThisOrderItems.Add(item.Product);
}
var model = new JDECheckoutCompletedModel
{
    // Other stuff...
    ProductList = ThisOrderItems
};

And then in the view:

foreach (var thisProduct in Model.ProductList)
{
    var url = Url.RouteUrl("Product", new { SeName = thisProduct.GetSeName() });
}

And here I still receive the error that "Nop.Core.Domain.Catalog.Product does not contain a definition for GetSeName".

I know I must be making a rookie mistake, right?
6 years ago
Add @using Nop.Services.Seo

GetSeName is an extension function.
6 years ago
That was the answer.  I had no idea!  I added that and all is well.

Now I'm trying to figure out how to do it the correct MVC way by getting the data in the controller, but at least it works and the deadline is met.

Thanks!
6 years ago
ChuckR wrote:
wooncherk gave the correct answer above.  To be clear, you "can" do a direct data dip from a view to a data store. Previous to MVC methodology, (classic asp) - this was normal practice to do calculations, data fetches, etc. right in your presentation layer.
But the whole point of MVC pattern is to separate this out. If your model is missing the seName, simply add it, and in your controller make sure it gets populated into your model.
Within the controller, you already did something like getproductbyid...and now you would repeat the entire fetch again while trying to render the page which is extremely unnecessary.
At the end of the day, your question really isn't about nop, its clarifying transition from legacy asp to MVC.  The answers to most questions on these forums is what "Should" you do, not what "Can" you do.

All that being said, nop does use an MVC approach, and if you don't like the MVC pattern, you can break the rules as your asking, and do data dips on your views.

thx


I just wanted to jump back in here and say that I added the SeName to my model. The view no longer asks for new data: it just gets the SeName from the model.  Now that I have a dull understanding of the MVC approach, I see plenty of other stuff here that I could optimize.  Thanks!
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.