There is such a model.

public class FormModel { public string FormTitle { get; set; } public string FormDescription { get; set; } public ICollection<BlockWorkingFieldForm> Fields { get; set; } } public class BlockWorkingFieldForm { public string FieldHeader { get; set; } public string FieldType { get; set; } public string Field { get; set; } public bool FieldQuestion { get; set; } } 

And there is a form, it consists of two parts.

  1. FormTitle and FormDescription are normal input fields.
  2. And BlockWorkingFieldForm is a block (input, chexbox and select), which can be added as many times as necessary (adding occurs at the bottom of the form, that is, not arbitrarily) and delete the created blocks in a random order. However, at least one BlockWorkingFieldForm must be present. Well, actually, when you click the Submit button, the form should go to the server and fill in the model.

On the client side only jQuery, but you can use other tools, if it helps to solve the problem.

I certainly found one way described in this article ( https://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/ ), but the article is already 10 years old, are there any other ways ?

    1 answer 1

    You can use razor in a view. Thus, you can create dynamic pages.

    1) First, create an intermediate model for your view, for example MyViewModel

      public class MyViewModel { public FormModel FormModel { get; set; } public BlockWorkingFieldForm BlockWorkingFieldForm { get; set; } public List<BlockWorkingFieldForm> listBlockWorkingFieldForm = new List<BlockWorkingFieldForm>(); } 

    Using the MyViewModel model, you will transfer 2 models to your view + collection, in which you will need to put all BlockWorkingFieldForm .

    2) Next, at the touch of a button, add data, and with the help of foreach display the collection on the presentation.

     @{ foreach(BlockWorkingFieldForm item in listBlockWorkingFieldForm) { . . . ваши input, chexbox и select например <input asp-for="@item.FieldHeader"/> . . . } }