Home > front end >  how to access model list data in view
how to access model list data in view

Time:09-15

I am creating a list in my controller and accessing it in view. I want to access the list data through for only loop not foreach loop this is my controller & view code

List < ViewModel > list = new List < ViewModel > ();
ViewModel model = new ViewModel();
string[] str1 = My name is Md Mohtesham;
model.a = str1[0];
model.b = str1[1];
model.c = str1[2];
model.d = str1[3];
model.e = str1[4];
list.add(model)
return View(list)
@model List<FD_Application.Models.ViewModel>
  <table >
    <tr>
      <th>a</th>
      <th>b</th>
      <th>c</th>
      <th>d</th>
      <th>e</th>
    </tr>

    @for(int i = 0; i < 5; i  ) 
    { <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      </tr>
      }
  </table>

CodePudding user response:

You just need to change in your loop with Model with that you can access your supplied list like below:

@model List<FD_Application.Models.ViewModel>
  <table >
    <tr>
      <th>a</th>
      <th>b</th>
      <th>c</th>
      <th>d</th>
      <th>e</th>
    </tr>

    @for(int i = 0; i
    < Model.Length; i  ) { <tr>
      <td>Model[i].a</td>
      <td>Model[i].b</td>
      <td>Model[i].c</td>
      <td>Model[i].d</td>
      <td>Model[i].e</td>
      </tr>
      }
  </table>
  • Related