Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to show all data in a list using EF and MVC

What I have tried:

<pre>i write a code to show a list
 
<table>
<tr>
<th></th>
<th>TeleGUID
</th>
<th>Screen Name
</th>
<th>Mobile Number
</th>

</tr>


@foreach (var item in Model)
{

<tr>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.TeleGUID }) %> |
@Html.ActionLink("Details", "Details", new { id = item.TeleGUID })%>
</td>
<td>

@Html.DisplayFor(modelItem => item.TeleGUID)
</td>

<td>

@Html.DisplayFor(modelItem => item.TeleName)
</td>
<td>

@Html.DisplayFor(modelItem => item.TeleMob)
</td>

</tr>
}

</table>
 
 
 
 
controller code is 
 
[HttpGet]
public ActionResult UserDetails()
{
UserdbEntities db = new UserdbEntities();
var tasks = from t in db.UserInfoes
select new
{
t.GUID,
t.Name,
t.Mob,
};

return View(tasks.ToList());
 
 
but i got an error
Posted
Updated 24-Aug-17 0:35am
Comments
Richard Deeming 25-Aug-17 14:05pm    
If you want someone to help you fix an error, then you need to tell us what that error is!

Click "Improve question" and update your question with the full details of the error. Remember to indicate which line of code it relates to.

1 solution

Depending on what you want to do and where the data for the list is coming from, Using your model, create a list in your controller, see example bellow:
Public ActionResult ActionName ()
{
    List <modelName> list = new List<modelName>()
{// populate the list here
}
/*or if u getting the data from database db context */
DbContextName db = new DbContextName();
/* and get the list from db using LiNQ, the return the model like
 */
var data = from x in db.modelName select x;
Return View (data);
}

I'd suggest you create a strongly typed view
 
Share this answer
 
Comments
aiswarjya1 25-Aug-17 7:11am    
this code is not working
aiswarjya1 26-Aug-17 3:11am    
i got error to use this code
aiswarjya1 7-Sep-17 3:23am    
data fetch in controller but i got the error from .chtml page
the error is

he model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MvcApplication3.TeleInfo]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MvcApplication3.Models.MTeleInfo]'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900