Danish
Danish

Reputation: 11

how to display all the data from database in MVC5 views

  public class EmployeeController : Controller
{
    //
    // GET: /Employee/

    public ActionResult Details(int id)
    {
        EmployeeContext employeeContext = new EmployeeContext();

        Employee employee = employeeContext.Employees.SingleOrDefault(x => x.EmployeeId == id);
        return View(employee);
    }

}   

//here i am getting only one row with respect to id .. how can i get the whole rows from the table ?and how can i view that on the view page?

Upvotes: 1

Views: 1573

Answers (1)

Nick Larsen
Nick Larsen

Reputation: 18877

To return the whole table, just select the whole table instead of filtering.

Employee[] allEmployees = employeeContext.Employees.ToArray();

To show them in a view, just have the model type for your view be an array instead of just a singular Employee. Basically just make the model Employee[]. Then you can just loop over the model.

@model Employee[]
@foreach(var employee in Model)
{
    // do something interesting
}

Upvotes: 2

Related Questions