使用EF在MVC中列出数据 [英] Listing data in MVC using EF
本文介绍了使用EF在MVC中列出数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用EF和MVC显示列表中的所有数据
我尝试过:
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
推荐答案
取决于你想做什么以及在哪里列表的数据来自,使用您的模型,在控制器中创建一个列表,请参见下面的示例:
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
这篇关于使用EF在MVC中列出数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文