[英]View data from database using MVC & Entity Framework
I am trying to view list of items from database ( I am using Entity Framework). 我正在尝试查看数据库中的项目列表(我正在使用Entity Framework)。
My Repository method: 我的存储库方法:
public List<string> getListOfItems(int i)
{
return (from x in db.Items where x.ID == i select x.Text).ToList();
}
My Controller: 我的控制器:
public ActionResult Index()
{
var itemOutput = repo.getListOfItems(1); // I just put 1 since I didn't know how to specify "i" - However theoretically it should return first item in database but its not
ViewBag.itemOutput = itemOutput ;
return View();
}
Model: 模型:
public class items
{
[Key]
public int ID{ get; set; }
public string name{ get; set; }
public string quantity{ get; set; }
}
ItemModel: ItemModel:
public class itemModels
{
public List<List<string>> itemData{ get; set; }
}
View: 视图:
@foreach (var item in ViewBag.itemOutput )
{
<table id="t01">
<tr>
<td>@item.name</td>
</tr>
</table>
}
ViewBag.itemOutput
is a List<string>
which makes item
a string
. ViewBag.itemOutput
是一个List<string>
,它使item
成为string
。
Therefore, use @item
instead of @item.name
(as string
does not have .name
property) in your view: 因此,在视图中使用@item
而不是@item.name
(因为string
没有.name
属性):
@foreach (var item in ViewBag.itemOutput )
{
<table id="t01">
<tr>
<td>@item</td>
</tr>
</table>
}
Also, to get the full list, you could do: 另外,要获取完整列表,您可以执行以下操作:
public List<string> getListOfItems()
{
return (from x in db.Items select x.Text).ToList();
}
And then just call getListOfItems()
with no param. 然后只需调用不带参数的getListOfItems()
。
1) Don't use plural for class name, unless the class is somewhat a collection of things 1)不要使用复数作为类名, 除非该类有点东西的集合
--> public class item // without s
2) You said in comments that items
are full varchar
which is untrue as per your class definition (you have ID
, name
& quantity
). 2)您在评论中说items
是完整的varchar
,根据您的类定义(您具有ID
, name
和quantity
),这是不正确的。
3) Using string
for quantity
is a bit weird. 3)使用string
作为quantity
有点奇怪。
4) You could indeed change your getListOfItems
method to: 4)您确实可以将getListOfItems
方法更改为:
public List<item> getListOfItems()
{
return (from x in db.Items select x).ToList();
// which can be simplified to:
// return db.Items.ToList();
}
but you should then change your view back to @item.name
. 但是您应该将视图更改回@item.name
。
This would however allow you to do: 但是,这将允许您执行以下操作:
@foreach (var item in ViewBag.itemOutput )
{
<table id="t01">
<tr>
<td>@item.name</td>
<td>@item.quantity</td>
</tr>
</table>
}
5) You have an ItemModel
but you are not using it. 5)您有一个ItemModel
但没有使用它。 You could modify it and use it instead of the ViewBag
. 您可以修改它并使用它来代替ViewBag
。
getListOfItems() returns a list of string, but you're referring to an actual object in ViewBag.itemOutput getListOfItems()返回字符串列表,但是您引用的是ViewBag.itemOutput中的实际对象
Instead of select x.Text do a select x, and make the return value List<items>
代替select x.Text,请选择select x,并使返回值List<items>
public List<items> getListOfItems(int i)
{
return (from x in db.Items where x.ID == i select x).ToList();
}
Then you can keep your razor template the same to refer to @item.name 然后,您可以将剃刀模板保持不变以引用@ item.name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.