Loop through Model property with Razor in MVC View

for loop in mvc view
mvc view loop through model
mvc foreach item in model
mvc iterate through list in view
mvc foreach property in model
for loop in model mvc
for loop in mvc controller
mvc loop through model properties in controller

I have this Model with a lot of properties

public class Task {
    public string Key { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    ....
}

I need to create a table in the View to list all the properties of the Task.

The same question was already asked but I don't want to use ViewData: Looping through view Model properties in a View

Any idea?

Does this work for you?

@foreach(var task in Model) {
    <tr>
    @foreach(var property in typeof(Task).GetProperties()) {
        <th>@(property.GetValue(task, null))</th>
    }
    </tr>
}

Accessing MVC view model properties in a loop, MVC loop through model in controller. It turns out that the ModelMetadata property of ViewData dictionary object allows you to get or set the  MVC loop through model in controller It turns out that the ModelMetadata property of ViewData dictionary object allows you to get or set the information about the model bound to the view. For example, if I had a model, as shown in Listing 1 below, and a strongly-typed view based on this model (Listing 2), I could then access the ModelMetadata property and simply loop through its properties, as needed.

You probably need to use reflection to get the properties.

Something like this

@model List<Task>
@if(Model.Any())
{
   var propArr = Model.Events[0].GetType().GetProperties();
   foreach (var ev in Model)
   {
      var p = ev.GetType().GetProperties();
      foreach (var propertyInfo in propArr)
      {
         <h4>@propertyInfo.Name</h4>     
         var val = propertyInfo.GetValue(ev, null);
         if (val != null)
         {
             <p>@val</p>
         }
      }        
    }
}

Loop through Model items in ASP.NET MVC, To loop through Model items in ASP.NET MVC view, use foreach loop in the Controller,which returns Collection of items. Add the given below line  I want to loop through each item in my model in my razor view but I want to group all items together. I then want to loop through each group. Imagine I have a table: ID GroupNo GroupName 1 1 Group1 2 1 Group2 3 1 Group3 4 2 Group1 5 2 Group2 6 3 Group56

please use this

@foreach (var task in Model)
{    
    <tr>

        <td>
          @task.Title 
        </td>

    </tr>
}

and so on for the other properties. Then in your controller u return a list of task like

`return View(your taskList);`

Loop through Model items in ASP.NET MVC ~ DotnetPiper, NET MVC view, use foreach loop in the Controller,which returns Collection of Precompiled Razor View Using RazorGenerator MVC and  Loop through Model items in ASP.NET MVC. To loop through Model items in ASP.NET MVC view, use foreach loop in the Controller,which returns Collection of items. Add the given below line to bind your model with .cshtml page.

Expanded on Steve Coopers Example:

    <table class="table table-bordered table-responsive table-hover">
                        @foreach (var item in Model.a)
                        {
                            <tr>
                               @foreach (var prop in typeof(a).GetProperties())
                               {
                                   <th>@(prop.Name)</th>
                               }
                            </tr>
                            <tr>
                                @foreach (var prop in typeof(a).GetProperties())
                                {
                                    <td>@(prop.GetValue(item, null))</td>
                                }
                            </tr>
                        }
                    </table>

Using the Razor syntax, enumerate the view's Model property , -- video games --> HTML comment. It asks me if I used the view's Model property as the source for the foreach loop. I did it exactly as in the video,  So I prefer to use an indexed for loop (over the simplicity of a strongly typed for each loop): @For i = 0 To Model.Articles.Count - 1 Dim curItem = Model.Articles(i) @Html.EditorFor(Function(model) curItem) Next Further Reading: Introduction to ASP.NET Web Programming Using the Razor Syntax (Visual Basic) | C#

ASP.NET Razor C# Loops and Arrays, The for each loop walks through a collection until it is finished. The example below walks through the ASP.NET Request.ServerVariables collection. Using the Razor syntax, enumerate the view's Model property (containing an array of VideoGame object instances) within the provided <ul> element. Use a foreach loop to render each VideoGame object's DisplayText property value within its own <li> element. Name your loop value variable videoGame.

ASP.NET MVC Tutorial, Learn the true power of ASP.NET MVC default model binding and to the view to get the Razor engine to compose the HTML properly. field for a single CustomerID and the navigation property Customer . For the second tier data, the list of orders, we're looping through the records in the view model. This is normal behavior. The model binder uses the name property of the input elements from the rendered html. Using a for loop with an indexer is the only way to have unique names that the model binder can associate to the model – Forty-Two Jan 4 '13 at 22:00

razor syntax reference for ASP.NET Core, Learn about Razor markup syntax for embedding server-based code into webpages. Templated HTML can be rendered with looping control statements. In C#, a using statement is used to ensure an object is disposed. @model. This scenario only applies to MVC views and Razor Pages (.cshtml). As I said, it's purely an educational exercise. I was building a simple project from a tutorial to familiarise myself with the ASP.NET version of how MVC works and thought to myself "If the whole point of using MVC is to separate the concerns, why must the views have intimate knowledge of every single property name on the model when conceptually all they want to do is 'Show values of all, or

Comments
  • return a list of Task objects from your action method to view and simply loop through it. What is stopping you ?
  • Do you want to list the properties or the values or the properties?
  • I am returning a list of Task but how can I loop through each property?
  • you want to loop through all the properties of each task item or items in the list of tasks?
  • Take a look at this post. I think is what you need.
  • I am not familiar with reflection but the method doesn't work for me
  • I wish I could your a for loop to go through the properties instead list one by one