简体   繁体   English

使用MVC和实体框架查看数据库中的数据

[英]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>
}

Answer: 回答:

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()


Random comments: 随机评论:

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 ,根据您的类定义(您具有IDnamequantity ),这是不正确的。

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.

相关问题 实体框架 - MVC - 将Model中数据库请求的数据类型(POCO对象)转换为View中使用的业务对象 - Entity Framework - MVC - Converting data types requested from database in Model (POCO objects) into business objects used in the View 使用实体框架在mvc中返回部分视图时从数据库填充下拉列表 - populating dropdown from database while returning partial view in mvc using entity framework 使用实体框架从数据库中检索数据 - Retrieving data from database using Entity Framework 实体框架和 MVC 5. 来自数据库的数据未显示在本地主机上 - Entity Framework and MVC 5. Data from database not shown on localhost 在视图中使用嵌套的foreach的MVC实体框架 - MVC Entity Framework using a nested foreach in view 使用实体框架从两个表中绑定数据网格视图 - Bind Data Grid View from two tables using Entity framework 如何使用 Entity Framework 6 从数据库中获取数据 - How to Fetch Data from database using Entity Framework 6 使用Entity Framework从数据库检索数据时出现WebAPI问题 - WebAPI issue while retrieving data from database using Entity Framework 使用实体框架6将数据从数据库存储到本地 - Store data from database to local using entity framework 6 使用实体框架移动和比较来自不同数据库的数据 - Moving and comparing data from different database using entity framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM