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>
2
  • 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 so that you know what to include in your question next time. Commented Oct 27, 2016 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. Commented Oct 27, 2016 at 13:28

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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
what is the problem, is it not returning the anything, giving you errors, or it does return some data but not what you expect ?
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
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.