Dynamic Generation of form fields in Razor Pages

Blooming Developer 276 Reputation points
2021-10-21T10:35:47.903+00:00

Hi

I want to generate dynamic input fields as shown below using razor pages.
when i click on the plus button its should add another set of fields with delete button. How to acheive this?
How to design Model class for this?
Any help would be appreciated. Thanks.

142465-image.png

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,139 questions
{count} votes

Accepted answer
  1. Rena Ni - MSFT 2,061 Reputation points
    2021-10-22T06:51:52.803+00:00

    Hi @Blooming Developer ,

    Here is a simple demo you can follow:

    Model:

    public class TestModel  
    {  
        public string Name { get; set; }  
        public int Age { get; set; }  
    }  
    

    View:

    @page  
    @model IndexModel  
    <button id="addRow" type="button" class="btn btn-info">Add</button>  
    <form method="post">  
        <div id="newRow">  
            <input type="hidden" id="total" value="0" />  
        </div>  
        <input type="submit" class="btn btn-primary"  value="Post" />  
    </form>  
      
    @section Scripts  
    {  
        <script>  
        $("#addRow").click(function ()  
        {  
            var rowCount = parseInt($("#total").val());  
            rowCount++;  
            $("#total").val(rowCount);  
            var html = '';  
            html += '<div id="inputRow">';  
            html += '<input type="text" name="[' + (rowCount - 1) + '].Name"  />';  
            html += '<input type="number" name="[' + (rowCount - 1) + '].Age"  />';  
            //add more inputs here...  
            html += '<button id="removeRow" type="button" class="btn btn-danger">Remove</button>';  
            html += '</div>';  
      
            $('#newRow').append(html);  
        });  
        $(document).on('click', '#removeRow', function ()  
        {  
            var rowCount = parseInt($("#total").val());  
            rowCount--;  
            $("#total").val(rowCount);  
            $(this).closest('#inputRow').remove();  
        });  
        </script>  
    }  
    

    Backend code:

    public class IndexModel : PageModel  
    {  
    
        public IActionResult OnGet()  
        {              
            return Page();  
        }  
    
        public void OnPost(List<TestModel> model)  
        {  
            //do your stuff...  
        }  
    }  
    

    Result:
    142770-sss.gif


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    Rena

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Blooming Developer 276 Reputation points
    2021-10-26T07:18:31.577+00:00

    Hi @Rena Ni - MSFT ,

    i have field which is of type selectlist (dropdownlist). The items are not showing in my dynamically generated selectlist.
    how can i render the selectlist item there in dynamically generated field?

    Thanks,
    Teena