How to send custom model from custom section in Product edit on save?

10 个月 前
Hi, I am writing again to maybe get an answer on how to send a custom model from my custom section in Product edit page when the page is saved.
Basically, with the help of this community I have been able to create my custom section on the Product and Category edit pages. But I want to now capture the saving (or delete) event, and get the model (data from my inputs) back, so I can save it to the database. How can I achieve this?
Using version: 4.60.3
10 个月 前
Look how it is done in Avalara plugin. See this event handler.
10 个月 前
And how does it know that I want to get my exact model out?
And also which IConsumer should I use??
10 个月 前
I suspect it would be done with pattern matching expressions using the switch expression:

/// <summary>
/// Handle model received event
/// </summary>
/// <param name="eventMessage">Event message</param>
/// <returns>A task that represents the asynchronous operation</returns>
public async Task HandleEventAsync(ModelReceivedEvent<BaseNopModel> eventMessage)
{
    //get entity by received model
    var entity = eventMessage.Model switch
    {
        CustomerModel customerModel => (BaseEntity)await _customerService.GetCustomerByIdAsync(customerModel.Id),
        CustomerRoleModel customerRoleModel => await _customerService.GetCustomerRoleByIdAsync(customerRoleModel.Id),
        ProductModel productModel => await _productService.GetProductByIdAsync(productModel.Id),
        CheckoutAttributeModel checkoutAttributeModel => await _checkoutAttributeService.GetCheckoutAttributeByIdAsync(checkoutAttributeModel.Id),
        _ => null
    };
10 个月 前
But do I get the whole model through or just the Id?? Because in the example it searches by ID every time. And I don't have an id, as the model will create new items in the database.
10 个月 前
Also now that I have tried it, it doesn't work. I set a breakpoint just under the if, and it didn't got hit when saving the product or category. Below is my code:

    public async Task HandleEventAsync(ModelReceivedEvent<BaseNopModel> eventMessage)
    {
        var httpRequest = _httpContextAccessor.HttpContext.Request;

        if (_settings.Enabled &&
            eventMessage?.Model is DetailViewExtensionModel model &&
            !string.IsNullOrWhiteSpace(httpRequest.Path) &&
            httpRequest.Method == "POST" &&
            (httpRequest.Path.StartsWithSegments("/Admin/Product") || httpRequest.Path.StartsWithSegments("/Admin/Category")))
        {
            
        }
    }
10 个月 前
Are you inheriting the BaseNopModel
as in
public record ConfigureModel : BaseNopModel
{
....
}
10 个月 前
public record DetailViewExtensionModel : BaseNopModel
10 个月 前
Set breakpoint at the if.  Does it even get there?  If so, examine the if conditions to see why it does not go inside.