c# - Passing a list to aview in asp mvc : List vs IEnumerable -


just quick question.

@using mvcapplication6.models; @model ienumerable<employee>  @{     viewbag.title = "index"; }  <h2>index</h2>  <p>     @html.actionlink("create new record", "create") </p>  <p>@html.actionlink("check departments", "index", "department")</p>  <table border="1">     <tr>         <th>             @html.displaynamefor(model => model.id)         </th>         <th>             @html.displaynamefor(model => model.firstname)         </th>         <th>             @html.displaynamefor(model => model.lastname)         </th>         <th>             @html.displaynamefor(model => model.gender)         </th>         <th>             @html.displaynamefor(model => model.department)         </th>         <th>action</th>     </tr>  @foreach (employee item in model) {     <tr>         <td>             @html.displayfor(modelitem => item.id)         </td>         <td>             @html.displayfor(modelitem => item.firstname)         </td>         <td>             @html.displayfor(modelitem => item.lastname)         </td>         <td>             @html.displayfor(modelitem => item.gender)         </td>         <td>             @html.displayfor(modelitem => item.department)         </td>         <td>             @html.actionlink("edit", "edit", new { id=item.id }) |             @html.actionlink("details", "details", new { id=item.id }) |             @html.actionlink("delete", "delete", new { id=item.id }) |             @html.displayfor(modelitem => item.players[0])         </td>      </tr>   }  </table> 

and how passed list:

  public actionresult index()     {          employeecontext emp = new employeecontext();         list<employee> adep = emp.employees.tolist();          return view(adep);     } 

why have declare ienumerable on view though 1 passing list ? if change ienumerable list, errors on line of code:

@html.displaynamefor(model => model.id) 

i testing on other views , it's declared list :

@model list<mvcapplication6.models.employee> 

it works intended although no displaynamefor , displayfor involved.

ienumerable
can use ienumerable<t> when need iteration (see here full list: https://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx), means if want list foreach, should return ienumerable<t>.

list
can use list<t> list of objects needs iterated through, modified, sorted, etc (see here full list: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx).


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -