Implementing AdminTabStripCreated is straightforward enough but how would I gain reference to the entity that the tab is created for...
For Example: A Category, I can generate a new tab for the "category-edit" tab strip but how would I know which category the tab strip was created for so I could populate my content with the appropriate information?
Does anyone have a working plugin sample they could share that uses this functionality to add custom data to a category or product?
I just came across the same problem as you did, but I think I found a solution. If you need the id of the category being edited, you can pull the details from the RouteData in the HttpContext.
Something like this: (I haven't tested this yet)
int CategoryId = Convert.ToInt32(System.Web.HttpContext.Current.Request.RequestContext.RouteData.Values["ID"]);
This is what I'm going to be doing to add a tab in the edit product view. Take a look in the RouteData.Values dictionary and you should find what you're looking for if it isn't ID.
I did see that the .Request, .HttpContext, and .ControllerContext properties are all null when handling the event, however System.Web.HttpContext.Current was not null. I was finally able to insert a custom tab and have it load a view by using the following code. It took a little while to get it all working but I found I had to manually set the ControllerContext to get the RenderPartialViewToString to work.
publicvoid HandleEvent(AdminTabStripCreated eventMessage) { //we know that tabstrip name on the product details page has "product-edit" name if (eventMessage.TabStripName == "product-edit") { int productId = Convert.ToInt32(System.Web.HttpContext.Current.Request.RequestContext.RouteData.Values["ID"]); ProductTabsAdminModel model = new ProductTabsAdminModel(); model.ProductId = productId; model.Tabs = _productTabsService.GetTabsForProduct(productId);
// Set the ControllerContext ifnull this.ControllerContext = this.ControllerContext ?? new System.Web.Mvc.ControllerContext(System.Web.HttpContext.Current.Request.RequestContext, this);
// Get the content for the tab string content = this.RenderPartialViewToString("Nop.Plugin.UI.ProductTabs.Views.agisProductTabs.ProductTabsAdmin", model);
This worked well for me too. There are a couple points that I did not get right away that I would like to clarify for future readers:
- The HandleEvent must go in your controller class, not a separate class (which some plugin examples use) - You must always assign the this.ControllerContext property in your controller as specified in the above post (even if you do not need to access the RouteData, etc.) otherwise the call to RenderPartialViewToString will not work.
I have found that this will add a querystring like: "/Admin/OrderSynchronization/OrderSynchronizationDetails?orderId=36046". But my controller is only available at "Admin/OrderSynchronization/OrderSynchronizationDetails?orderId=36046"