In an application being rewritten, originally a certain form included a drop down where the user chose one of two major options for how the input would be used in calculations elsewhere.
The requirements now dictate that instead of this drop down, the interface should feature two otherwise identical forms appearing on the same page, one above the other. A parameter or hidden value is to determine whether the aforementioned option is set on each of the forms so that there is one form for each of the two options. This sets a boolean value on the model.
I’m using the same partial view for both appearances of the form, defining their differences during initialization. However, I’m still having one particular issue–if there is a validation error on one form, it appears on both. What’s the best way to prevent this?
Am I just going to have to give in and make near-duplicate partial views, or is there a way to keep using the same one?
You could try something like this:
Create a base model for the form. That base model will have the properties and validation attributes that are common to its two child models:
You can then create two different controller actions that accept those models as parameters:
And your partial view would have to add an input depending on which form it is:
When a form gets posted, it should only do validation on its model and leave the other model untouched (you’d need a view model that has both form models as properties).
I’m not sure if any of this would work, but, again, its something you could try.