How can I get the product URL in my view?

1 2 >
Posted: July 13, 2017 at 5:22 PM Quote #190014
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.
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: July 13, 2017 at 5:40 PM Quote #190015
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.
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: July 13, 2017 at 9:57 PM Quote #190022
Assuming your model has SeName property,

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


This post/answer is useful
1
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Lam Woon Cherk
https://www.pronopcommerce.com

Sign up to my mailing list for tons of tips on nopCommerce: http://bit.ly/2fg5fMy

HINT: Upon signing up, you'll receive my e-book "nopCommerce Speed Optimization" on
how to make your nopCommerce websites score A in Google PageSpeed Insights
Posted: July 14, 2017 at 7:52 AM Quote #190049
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(" ", "-")
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: July 14, 2017 at 5:49 PM Quote #190066
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
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: July 14, 2017 at 6:45 PM Quote #190067
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.
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: July 14, 2017 at 6:46 PM Quote #190068
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?
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: July 14, 2017 at 6:51 PM Quote #190069
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();
This post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: July 15, 2017 at 8:49 AM Quote #190087
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
This post/answer is useful
2
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: July 16, 2017 at 10:35 PM Quote #190130
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 post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
1 2 >
Premium support services
  • Dedicated premium support services provided by core developers are intended for persons who run mission critical websites, work on projects with tight deadlines, or want to get dedicated support.
Professional services
  • Want to open a new store? Want to take your store to the next level? Need a custom extension? We can customize nopCommerce to fit your store perfectly. Request a quote to get started.