我找到数据库实体参考?

时间:2016-10-27 13:12:13

标签: c# sql-server asp.net-mvc

我已经为现有的SQL DB创建了一个数据库实体。

但查询不会返回我期待的数据

namespace WebApplication5.Controllers
{
    public class EmployeeController : Controller
    {
        // GET: Employee
        public ActionResult Index()
        {
            List<string> orderIDs = new List<string>();

            using (var db = new WebApplication5.Models.BTP_NYAEntities())
            {
                var query = from b in db.FilledOrders
                            select b;

                //Console.WriteLine("All blogs in the database:");
                foreach (var item in query)
                {
                    //Console.WriteLine(item.OrderID);
                    orderIDs.Add(item.OrderID.ToString());
                }
            }

            ViewData["MyData"] = orderIDs;

            return View(orderIDs);
        }
    }
}

查看:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Employee Index Page</title>
</head>
<body>
    <div>
    <h1>Employee Index Page</h1>
        @{ 
            var categories = (List<string>)ViewData["MyData"];

                foreach(var item in categories)
                {
        <p>This is a test emp index page @item</p>

                }



        }
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您不应该从View中调用数据库。在控制器操作或业务逻辑中调用此函数,然后使用viewbag将列表传递给视图

按照this Question

中的方法操作