Reputation: 169
Fairly new to MVC Core and have a View which contains two forms and a single button within one of the form tags. I have a ViewModel so that I can pass multiple objects in and out of the View.
The problem I am seeing is the button only POSTs back the contents of the form it's within and not the data contained within the other form on the same View, even though it populates the ViewModel correctly; I have tested this by putting a button inside each form and depending on which button you have clicked, that form's data is passed back to the Controller.
As a newbie, I assumed that once the property within the ViewModel had been populated the POSTing back of the submit button would capture all entered data - this is not the case.
The examples I have seen seem to pertain to having the requirement for different functions within the same View (eg. Login/Register) but I would like all the data collected to be passed back in one POST, is this possible?
If you want some source, I can submit but I think the above hopefully explains my problem well enough.
TIA.
Upvotes: 2
Views: 422
Reputation: 169
Going with agua's answer although it didn't expressly resolve the issue (if indeed it is solvable).
Upvotes: 0
Reputation: 941
This is not a problem related to ASP as this runs on your server and generates the html code delivered to the browser. It's the browser who posts back the data ... and hence when a form is submitted, the browser collects the contained input fileds and creates the post request from their data. In fact, the browser doesn't know anything about viewmodels.
You might work around that by generating hidden input fields into each form for the viewmodel's locally unused properties. This way both forms contain all data and the resulting post will be 'complete'. But from my experience, that is ugly to maintain. So I'd recommend you should consider using separate forms with separate post endpoints.
Upvotes: 2