0

In my MVC4 project I have used an Editor Template for user input. I have define a constant(say 4) and called it in a for loop, so there is always a constant number of 4 templates displayed on the view.

I want to change this and make it dynamic, ie when the user clicks on a button, a new Editor Template should be displayed dynamically. When he again clicks on the button, a third one will be displayed and so on.
Do I need to use angularJS for this or is it possible to do it in .net itself? How do I achieve this? Thanks.

Sumedha Vangury
  • 643
  • 2
  • 17
  • 43
  • You can use the [BeginCollectionItem](https://www.nuget.org/packages/BeginCollectionItem/) helper - refer example [here](http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/). If you want do dynamically add a new item, use ajax to call method which returns the partial view. –  Sep 03 '15 at 12:48
  • @StephenMuecke the example shows a partial view rendering a list editor and not an editor template. Also the article was last updated in 2010 and is a bit old. – Sumedha Vangury Sep 03 '15 at 13:06
  • You cant use an `EditorTemplate` to dynamically add new items - it will not include the correct indexers for binding to a collection (and an `EditorTempate` is just a partial view that is named to match the model type anyway). Yes its old but still relevant. And some other examples of doing this all using client side side code [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) and [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) –  Sep 03 '15 at 13:10
  • @StephenMuecke its a bit complicated, I am trying to understand. – Sumedha Vangury Sep 03 '15 at 13:26

0 Answers0