0

Recently I have switched from JSF to ASP.NET and I'm missing an important function: The ViewScope. I want to reproduce the behaviour of a ViewScoped controller.

example: I have a list of names and a page that shows an input field for every name in that list that lets you edit them. At the bottom is a button that adds a new empty item to the list.

In JSF this would be pretty easy. Set the controller to @ViewScoped and link the button to a method that adds a new empty item to the list and then rebuild the view. The new item would then be there in the form of an empty input field.

As far as I know, ASP.NET only lets you save data in the session storage. I would prefer not to use that because I want the empty object to vanish in case the user changes the page without hitting the save button.

I could add the input field via JavaScript but then I don't know how to retrieve the data in the controller. In my actual application there are a lot of input fields that need to be added, so working with JavaScript would be difficult. Additionaly I would have to update the page as well as the Javascript in case someting changes.

I would really prefer to do this on the server side.

How could I approach this problem?

Kendoha
  • 99
  • 11
  • 1
    Some options for dynamically adding form controls [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) and [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) - using the `BeginCollectionItem` helper is what you probably want –  Sep 22 '15 at 13:09
  • Not familiar with JSF, but based on some light reading you may be looking to leverage TempData. Otherwise, submit what you have to the action, modify it and re-output. When you're ready to save then you can modify the session/database/etc. Until then, work with the incoming ModelState/Model. – Brad Christie Sep 22 '15 at 13:49
  • how long is the lifecycle of of TempData? – Kendoha Sep 23 '15 at 05:38

0 Answers0