Model CustomProperites - not working with ModelBinder or Nop-Editor tags

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
1 anno tempo fa
I believe this is a major bug as of Nop v4.50.2

It appears that without a lot of work the "CustomProperties" of various models (i.e. Store, Customer, etc.) do not work by themselves.

1. The first problem I ran into is that when a form is posted back to a controller, those custom properties are not processed and do not exist on the model.  Despite verifying that these values are posted in the POST DATA, the ModelBinder does not process those values.

I was able to fix this issue by following these instructions, although they are a little outdated:

https://www.nopcommerce.com/en/boards/topic/89966/modelbinder-problem-for-basenopmodelcustomproperties-object-dictionary

2. The second issue is that none of the "nop-editor" or "lablel asp-for" tags will work with these custom properties.  So far I have not solved this problem.  The only way I've been able to utilize the CustomProperties is by manually writing all the HTML code for these pages.

Ideally there is a solution, but if not, this needs to be addressed in an upcoming version of Nop.  I would think it would be easy to implement fixes for both of these but as a rule, we don't like to modify the Nop core code.
1 anno tempo fa
ScottMc101 wrote:

1. The first problem I ran into is that when a form is posted back to a controller, those custom properties are not processed and do not exist on the model.  Despite verifying that these values are posted in the POST DATA, the ModelBinder does not process those values.

There is indeed a problem with CustomProperties binding. This has been observed since we moved to .net core. There is probably no better way to fix this problem other than through a custom model binder. I have created a work item where you can track progress. However, we have decided to remove the CustomProperties property from the model in future releases and mark it as [Obsolete] for now. This is because there is a more general approach for passing specific data from the model to the controller - using the TempData container.


ScottMc101 wrote:

2. The second issue is that none of the "nop-editor" or "lablel asp-for" tags will work with these custom properties.  So far I have not solved this problem.  The only way I've been able to utilize the CustomProperties is by manually writing all the HTML code for these pages.

This use of CustomProperties was not originally intended in nop-editor tag helpers. We leave this functionality for customization through plugins, especially considering that we plan to stop using CustomProperties.
1 anno tempo fa
Thanks - can you please explain what the "better" way to handle custom properties would be - specifically with an example?  I'm thinking it would be rather common for a developer to need the ability to add additional fields to an entity (i.e. customer/product/store/etc) and work with those products.

While I have this working, using the custom model binder and some "tricky" code through a filter that digs back into the "request" object, it doesn't seem that this is the best way to handle this kind of data.

Nop was built to be extensible so it should be easier to add properties to track additional data.
I could not find any good tutorial or example showing how to do this.

Any help would be appreciated.
1 anno tempo fa
You can use the TempData container, which is a property on the ControllerBase. Since this is a standard mechanism in ASP.NET MVC, you can find many examples of its use. Here is an example of an article that briefly outlines the scenarios for using TempData with specific examples.
1 anno tempo fa
You can also take a look at Avalara tax plugin, where we have implemented two different approaches to extend models.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.