ManInMoon
ManInMoon

Reputation: 7005

WHere do I find database entity reference?

I have created a database entity to an existing SQL DB.

But query does not return the data I am expecting

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);
        }
    }
}

View:

@{
    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>

Upvotes: 0

Views: 284

Answers (1)

Mhand7
Mhand7

Reputation: 537

you should not be calling the DB from the View. call this function in the controller action or in a business logic and then pass the list to the view using the viewbag

follow the methods in this Question

Upvotes: 1

Related Questions