Pre-populate form on page load

David Rector asked on June 21, 2019 20:35

On our MVC site, we want to be able to pre-populate a form on page load if the user has already filled it out.

I already have the field names and values ready to populate the form in a cookie. The form is in a widget zone, so I'm not sure how to hook into some sort of load event and match the control names to the values I have in the cookie to populate the fields.

Is this possible?

Recent Answers


Zach Perry answered on June 21, 2019 21:47

Is the form data only in cookies?

If the values are stored in the DB then you would retrieve them in the controller and set the values for the view model. Stack overflow

If the values are only in cookies, then the only way I can think of is add them to the form with js after the page loads.

0 votesVote for this answer Mark as a Correct answer

Michal Samuhel answered on June 22, 2019 00:03

Just adding to Zach, you should be even able to get cookies collection via HttpRequestBase Controller.HttpContext Property it wraps also HTTPRequest incoming (controllers are instantiated along with incoming requests), so you can in controller action read data from users cookies and pre-fill the form.

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.