how to display list of dynamic tables from controller on view in asp.net?

Muhammad Faheem 1 Reputation point
2021-05-04T03:55:12.24+00:00

how to display list of dynamic tables from controller on view in asp.net?

Developer technologies | ASP.NET | Other
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

1 answer

Sort by: Most helpful
  1. Chao Deng-MSFT 801 Reputation points
    2021-05-04T08:33:39.32+00:00

    Hi @Muhammad Faheem ,
    Do you want to return the list data from the controller and display it in the table?
    You can refer to the following demo.
    Model:

     public class Test  
        {  
      
            public int Id { get; set; }  
            public string Name{get; set; }  
            public int Age { get; set; }  
        }  
    

    Controller:

     public ActionResult Test()  
            {  
                List<Test> result = new List<Test>  
              {  
                  new Test{Id=1,Name="Test01",Age=12},  
                    new Test{Id=2,Name="Test02",Age=13},  
                      new Test{Id=3,Name="Test03",Age=14},  
              };  
                return View(result);  
            }  
    

    View:

    <table class="table table-bordered table-hover">  
        <thead>  
            <tr>  
                <th>Id</th>  
                <th>Name</th>  
                <th>Age</th>  
            </tr>  
        </thead>  
        <tbody>  
            @foreach (var item in Model) {  
            <tr>  
                <td>@item.Id</td>  
                <td>@item.Name</td>  
                <td>@item.Age</td>  
            </tr>  
            }  
      
        </tbody>  
    </table>  
    

    Result:
    83okD.png


    If the answer is helpful, please click "Accept Answer" and upvote it.

    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,

    ChaoDeng

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.