Search code examples
c#asp.net-mvcrazor

How to display data from model to razor page


I am using asp.net mvc , i need to display some data from Model to razor page in asp.net MVC Without using the foreach because i need display one time not duplicate.

So some data i need to display one time and the other data i will display it in the table using the foreach. My problem is in display data one time

This is my View :

This data i need to display it one time i do not how i do that without using the foreach

<p>
       Teachers : @Html.DisplayFor(model => item.Teachers.Name)

        <br />

        Class :  @Html.DisplayFor(model => item.Subgroups.Name)

    </p>

Thank you


Solution

  • So you have a List attached to the view correct? Then you should navigate to a specific index in the List then access its property.

    If you want the first record in the list then;

    @Html.DisplayFor(model => model.FirstOrDefault().Teachers.Name)
    @Html.DisplayFor(model => model.FirstOrDefault().item.Subgroups.Name)
    

    If you know the specific index then; (let's say 3rd record)

    @Html.DisplayFor(model => model.ElementAt(2).Teachers.Name)
    @Html.DisplayFor(model => model.ElementAt(2).item.Subgroups.Name)
    

    If you want to filter then get the record;

    @Html.DisplayFor(model => model.FirstOrDefault(m=>m.Property == "Filter").Teachers.Name)
    @Html.DisplayFor(model => model.FirstOrDefault(m=>m.Property == "Filter").item.Subgroups.Name)