0

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>
ManInMoon
  • 6,795
  • 15
  • 70
  • 133
  • 1
    The whole point of MVC is to separate your concerns. The View should only be concerned with rendering a UI. Have your controller call out to a database layer that retrieves the relevant entities and store them in model objects. Pass those model objects to the view from the controller. In any case, your question is incomplete. You'll need to show the declaration for `BTP_NYAEntities`. Read over [MCVE](http://stackoverflow.com/help/mcve) so that you know what to include in your question next time. – mason Oct 27 '16 at 13:23
  • @ManInMoon Is `BTP_NYAEntities` in the same MVC project or separate one? Also try searching across solution with that name. If you can find then, check the `namespace` defined in that file above class name and use that namespace with `using MyNamespace;` on top of file where you want to use it. – Siva Gopal Oct 27 '16 at 13:28

1 Answers1

1

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

Community
  • 1
  • 1
Mhand7
  • 527
  • 1
  • 3
  • 21
  • OK. I have moved it to the controller. But it still cannot see BTP_NYAEntities. And in the example he uses db.Invoices, but again db is not known in my controller. – ManInMoon Oct 27 '16 at 13:23
  • from what I get, you followed the 3rd answer, instead of db you use your db context which is in your case BTP_NYAEntities. and then as you see that in the view you have to include the model that you want to use inside the view using @model – Mhand7 Oct 27 '16 at 13:37
  • what is the problem, is it not returning the anything, giving you errors, or it does return some data but not what you expect ? – Mhand7 Oct 27 '16 at 14:16
  • There is no error, but there is no data collection of rows or items, so the foreach loop does not get entered. But I can see there is actually data in table – ManInMoon Oct 27 '16 at 14:23
  • Acutually I found the error. I have different Shemas and FilledOrders exists in 2 different Schemas. When building the connection VS seems to have renamed theses table as FilledOrders ad FIlledOrders1. I was looking at the wrong one... How can I specify the Schema name from within the Controller? – ManInMoon Oct 27 '16 at 14:36