Search code examples
c#asp.neteventsmodel-view-controllernopcommerce

nopcommerce trying to add custom tab in admin panel


Ok so for the category edit page I have added a custom tab in the nopcommerce admin panel, so I'm trying to do the same for the manufacturer edit page but the content of the tab is not appearing.

admin image

Here is the code that I'm using:

using System.Web.Mvc;
using System.Web.Routing;
using Nop.Services.Events;
using Nop.Web.Framework.Events;

namespace Nop.Plugin.Widgets.Modifications.Events
{
/// <summary>
/// This class is used to detect when a Manufacturer is being edited in order to add a new tab for attachments.
/// </summary>
public class ManufacturerTabConsumer : IConsumer<AdminTabStripCreated>
{
    public void HandleEvent(AdminTabStripCreated eventMessage)
    {
        if (eventMessage.TabStripName == "manufacturer-edit")
        {
            //Manufacturer Attachments tab
            var manufacturerId = eventMessage.Helper.ViewContext.RequestContext.RouteData.Values["Id"];

            var urlHelper = new UrlHelper(eventMessage.Helper.ViewContext.RequestContext)
                .RouteUrl("Nop.Plugin.Widgets.Modifications.Admin.ManufacturerAttachments",
                    new RouteValueDictionary { { "manufacturerId", manufacturerId } });

            eventMessage.BlocksToRender.Add(new MvcHtmlString("<script>" +
                                                              "$(document).ready(function() {" +
                                                              "var tabStrip = $('#manufacturer-edit').data('kendoTabStrip').append({" +
                                                              "text: 'Attachments'," +
                                                              "animation: { open: { effects: 'fadeIn'} }," +
                                                              "contentUrl: '" + urlHelper + "'" +
                                                              "});" +
                                                              "});" +
                                                              "</script>" +
                                                              "<style>.k-link {font-weight: bold;}</style>"));
        }
    }
}

}

When I set a break point and get to this bit of code it tells me var urlHelper is null, though I'm not sure why.

var urlHelper = new UrlHelper(eventMessage.Helper.ViewContext.RequestContext)
                .RouteUrl("Nop.Plugin.Widgets.Modifications.Admin.ManufacturerAttachments",
                    new RouteValueDictionary { { "manufacturerId", manufacturerId } });

Anyone know why that might be null? Thanks.


Solution

  • Omg of course right as I post my question I see the problem, I forgot to change the code from the route I copied. Sometimes copy and paste is my worst enemy.

    Had to change categoryId to manufacturerId like this:

    routes.MapLocalizedRoute("Nop.Plugin.Widgets.Modifications.Admin.ManufacturerAttachments", "Plugins/Admin/ManufacturerAttachments/{manufacturerId}",
                new { controller = "ModificationsWidget", action = "ManufacturerAttachmentsAdmin" },
                new { categoryId = @"\d+" },
                new[] { "Nop.Plugin.Widgets.ModificationsWidget.Controllers" });
            route9.DataTokens.Add("area", "admin");
    

    This new { categoryId = @"\d+" }, to this new { manufacturerId = @"\d+" },

    Sorry folks, don't know how I missed that :(