There is an alternative:
- Add Prefix to PartialView
- Snap model by removing prefix
For 1, set the prefix in your view:
ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "prefix";
For 2, you can restore data using UpdateModel , for example:
UpdateModel(producto, "prefix");
This is not very desirable, because your action does not receive data as a parameter, but later updates the model. This has several inconveniences: 1) it is not clear what your action needs, looking at its signature. 2) It is not easy to provide input for action for unit testing. 3) the action is vulnerable to overflow parameters (parameters provided by the user that should not be there and mapped to the model).
However, there is an alternative for 2: register a custom mediator that allows you to remove the prefix. And the custom binder model needs to know about it.
A good solution to this is SO Q & A: How do I handle the MVC model binding prefix with the same shape repeated for each row in the collection? But it has a small drawback: if you add a hidden field named "__prefix" in the partial view, and you render it several times as a partial view, this identifier will be repeated for several different elements on the page, which is unacceptable, and can cause some problems . And one of the most important reasons for providing a prefix is ββthe same as editing, as partial views for multiple instances of an object. That is, this will happen on a page like gmail, where you can edit several emails at once.
There are several possible solutions to this problem.
One of them provides a prefix in the form of a query string or a routedata value, and not as a form field that avoids identifier conflicts and can be found using a binder. (He can always have the same name).
Another solution is to use a hidden field with a fixed template, but for each rendered view it is different. The prefix could follow this pattern for uniqueness: "PP $ ActionControllerId", like "PP $ EditProduct23", which is unique for each rendered view and can be easily found between query parameters that start with "PP $".
And the final solution would be to create a prefix only in the view and not provide it in any query parameter. The modelβs connecting device should look for a prefix that checks the names of the request parameters until it finds one whose prefix matches the pattern.
Of course, the custom ModelBinder must be adapted to work on the selected agreement.