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
I have an ICollection of Nop.Core.Domain.Orders.OrderItem and I'm getting data from it in my cshtml.  I need to get the URL for each product in the order, but URL is not part of this collection.  I've seen some stuff around the web about the SeName, but I can't figure out how to get it from here.  

Here is an example of my code:

@foreach (var item in nopOrder.OrderItems)
        {
            <ul>
                <li>id: @item.Id</li>
                <li>name: @item.Product.Name</li>
                <li>url: no idea</li>
                <!-- and so on -->
            </ul>
        }


How can I get the URL for the product?

Thanks in advance.
6 years ago
The URL for the product is on the SEO tab (you have to click on the Advanced button on your product page to see it).  That value is stored in a table called UrlRecord, and is tied to the Products table by an ID field (I can't remember which fields tie together at the moment).  So, just do a left join from Products to UrlRecord and you can bring the URL into your code.
6 years ago
Assuming your model has SeName property,

@Url.RouteUrl("Product", new { SeName = Model.SeName })


6 years ago
SeName isn't part of the model and I can't figure out how to make this happen in the controller, but I did find that the following creates reliable URLs:

@{
var webHelper = Nop.Core.Infrastructure.EngineContext.Current.Resolve<Nop.Core.IWebHelper>();
var storeUrl = webHelper.GetStoreLocation();
}

url: @[email protected]().Replace(" ", "-")
6 years ago
It turns out this is not always correct.  Is there any way to do this solely in the view?  Since SeName is not part of the model, I can't just call on Model.SeName in my view.

Jeremy
6 years ago
Lecher wrote:
The URL for the product is on the SEO tab (you have to click on the Advanced button on your product page to see it).  That value is stored in a table called UrlRecord, and is tied to the Products table by an ID field (I can't remember which fields tie together at the moment).  So, just do a left join from Products to UrlRecord and you can bring the URL into your code.


Would I do this with LINQ in my view?  I'm not sure.
6 years ago
wooncherk wrote:
Assuming your model has SeName property,

@Url.RouteUrl("Product", new { SeName = Model.SeName })




What if the model does not have SeName as a property?
6 years ago
Why is it not as easy as this?

var productHelper = Nop.Core.Infrastructure.EngineContext.Current.Resolve<Nop.Services.Catalog.IProductService>();
var product = productHelper.GetProductById(item.ProductId);
var productSeName = product.GetSeName();
6 years ago
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
6 years ago
I totally understand that.  There's also "doing it the right way" and "there's an immediate deadline that requires getting it out the door any way possible."

I don't actually have a getproductbyid functionality in this controller.  The model is just an order number, a boolean for OnePageCheckout, an order, and an order detail.  I definitely will see if I can figure out how to do this the right way, but I also need to meet the deadline.  

Thanks!
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.