FormValueRequired filter does not work in plugin

7 months ago
I extend OrderController in plugin

like:

    namespace Nop.Plugin.Invoice.Controllers
    {
        public partial class InvoiceOrderController : OrderController
        {              
            ...
            [HttpPost, ActionName("Edit")]
            [FormValueRequired("invoiceorder")]
            public virtual IActionResult InvoiceOrder(int id)
            {
                ...
            }

        }
    }


The problem is with "FormValueRequired" in the plugin does not work.
The front end looks ok with the name of the button


<button type="submit" id="invoiceorder" class="btn bg-teal" style="margin-right: 3px;" name="invoiceorder">
  @T("Plugins.Invoice.Order.Invoice.Status.Invoice")
</button>


using: Nop4.2

Have any of you solved this problem?
7 months ago
I assume without the formvalue the method does not get hit. Can you please share your Edit.cshtml page of the plugin. There are 2 ways I normally use when try to override, add a new features and modify admin side functionalities 1. override the specific IAction method by actionfilter or override view and replcae the controller name of the view page by plugin controller name.
7 months ago
I override the UpdateCart function in a plugin and it works

The button
        <input type="submit" name="updatecart" value="@T("ShoppingCart.UpdateCart")" class="button-2 update-cart-button" />

The Form
        <form asp-route="ShoppingCart" method="post" enctype="multipart/form-data" id="shopping-cart-form">

The method in CustomShoppingCartController
        [HttpPost, ActionName("Cart")]
        [FormValueRequired("updatecart")]
        public override IActionResult UpdateCart(IFormCollection form)
        {
7 months ago
Yes, I have other overwritten methods and it works OK.
This case is with new functionality added by plugin.

In the FORM of the order detail there are several buttons that trigger the submit for update(post) the order.
The difference is who triggers the submit
IMG


I try to do the same with the Plugin Controller

PlugInController

[HttpPost, ActionName("Edit")]
            [FormValueRequired("invoiceorder")]
            public virtual IActionResult InvoiceOrder(int id)
            {
                ...
            }


DependencyRegistrar

public void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
        {
            ...            
            builder.RegisterType<InvoiceOrderController>().As<OrderController>();          
        }


The View

@model CustomOrderModel

@using Nop.Core.Domain.Orders;
@using Nop.Services
@using Nop.Plugin.Invoice.Domain;

<script>
    $(document).ready(function () {
        
    });
</script>

<div class="panel-body">
    <div class="panel panel-default sub-panel">
        <div class="panel-body">
            <div class="form-group">
                <div class="col-md-3">
                    <nop-label asp-for="OrderInvoiceStatus" />
                </div>
                <div class="col-md-9">
                    <div class="input-group input-group-short">
                        <div class="input-group-text">
                            <strong>
                                <div class="form-text-row">@Model.OrderInvoiceStatus</div>
                            </strong>
                        </div>
                        <div class="input-group-btn">
                            @if (Model.OrderInvoiceStatusId == (int)InvoiceStatus.Invoiced)
                            {
                                <button type="submit" name="cancelinvoiceorder" id="cancelinvoiceorder" class="btn bg-red" style="margin-right: 3px;">
                                    @T("Plugins.Invoice.Order.Invoice.Status.Cancel")
                                </button>
                                <nop-action-confirmation asp-button-id="cancelinvoiceorder" />
                            }
                            else if (Model.OrderInvoiceStatusId == (int)InvoiceStatus.Pending)
                            {
                                <button type="submit" id="invoiceorder" class="btn bg-teal" style="margin-right: 3px;" name="invoiceorder">
                                    @T("Plugins.Invoice.Order.Invoice.Status.Invoice")
                                </button>
                                <nop-action-confirmation asp-button-id="invoiceorder" />
                            }
                            else if (Model.OrderInvoiceStatusId == (int)InvoiceStatus.Error)
                            {
                                <button type="submit" name="reinvoiceorder" id="reinvoiceorder" class="btn bg-teal" style="margin-right: 3px;">
                                    @T("Plugins.Invoice.Order.Invoice.Status.ReInvoice")
                                </button>
                                <nop-action-confirmation asp-button-id="reinvoiceorder" />
                            }
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



from what I see the problem is between the FormValueRequired and the ParameterBasedOnFormNameAndValueAttribute filter, because the filter does not understand the FormValueRequired attribute in plugin controller.

But I don't know how to fix it. maybe copy the filter into the plugin
6 months ago
But if you do inheritance like this, the controller name (as in when the route value is parsed) is going to be invoiceorder instead of order. If your form is post to OrderController, I don't think InvoiceOrderController is going to pick up because it's a different route then.

I suggest using MVC Filter to do this instead. :)


6 months ago
Yes, InvoiceOrderController collects the requests to OrderController thanks to this in the dependencies registry.

builder.RegisterType <InvoiceOrderController> () .As <OrderController>

the problem is with "ParameterBasedOnFormNameAndValueAttribute" class that does not collect InvoiceOrderController attributes  (FormValueRequired)


[HttpPost, ActionName("InvoiceOrder")]        
        [FormValueRequired("invoiceorder")]
        public virtual IActionResult InvoiceOrder(int id)
        {
            ...
        }


In the next few days I will try to make a filter like "ParameterBasedOnFormNameAndValueAttribute" for the plugin