asp.net MVC filtering query

I'm using Visual Studi0 2013.
I have a ASP.NET MVC 5 application. I'm using C#, Razor , Entity Framework 6 database first model.

I'm using sql server 2008.

My table called Employees looks like this:

picture of Employees table
http://sqlfiddle.com/#!6/74449/1

I created a model called TestDatabase.edmx

My model looks like this:

Employee model
I created a controller called HomeController.cs using the scaffolding.

So my controller looks like this:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using NorthwindApp.Models;

namespace NorthwindApp.Controllers
{
    public class HomeController : Controller
    {

        private TestDatabaseEntities db = new TestDatabaseEntities();

        // GET: Home
        public ActionResult Index()
        {
            return View(db.Employees.ToList());
        }

        // GET: Home/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Employee employee = db.Employees.Find(id);
            if (employee == null)
            {
                return HttpNotFound();
            }
            return View(employee);
        }

        // GET: Home/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Home/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "EmployeeID,LastName,FirstName,Region")] Employee employee)
        {
            if (ModelState.IsValid)
            {
                db.Employees.Add(employee);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(employee);
        }

        // GET: Home/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Employee employee = db.Employees.Find(id);
            if (employee == null)
            {
                return HttpNotFound();
            }
            return View(employee);
        }

        // POST: Home/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "EmployeeID,LastName,FirstName,Region")] Employee employee)
        {
            if (ModelState.IsValid)
            {
                db.Entry(employee).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(employee);
        }

        // GET: Home/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Employee employee = db.Employees.Find(id);
            if (employee == null)
            {
                return HttpNotFound();
            }
            return View(employee);
        }

        // POST: Home/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Employee employee = db.Employees.Find(id);
            db.Employees.Remove(employee);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }


    }
}

Open in new window


my app directory looks like this:
app directory
The views on the Home folder where created by the scaffolding

When I run the app it runs fine and looks like this:

picture of Index.cshtml when I run app
If you notice, it is showing every record from my table.


So if you notice my Index ActionResult it looks like this:

        public ActionResult Index()
        {
            return View(db.Employees.ToList());
        }

So this actionresult is returning the full list of Employees to by Index.cshtml view

How do I revise my ActionResult so only the item for Region 1 are sent to the view?

So when I run my app the only items it should see are the items for region 1.
So when i run my page, it should look like this:

desired result
LVL 1
maqskywalkerAsked:
Who is Participating?
 
Fernando SotoRetiredCommented:
You want to filter at the server and not in local code so you should filter before the ToList.
return View(db.Employees.Where( r => r.Region == "1").ToList());

Open in new window

0
 
CamilliaCommented:
You have this

 public ActionResult Index()
        {
            return View(db.Employees.ToList());
        }

Open in new window


So, you need a where clause to limit the region to 1

Something like this maybe

var results =
   (from x in db.Employees
   where x.Region == 1 //if Region is an integer field
   select x).ToList();

 return View(results);

Open in new window


I think you can use a where clause as well

  return View(db.Employees.ToList().Where(x => x.Region == 1));

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.