I would like to have the index view (code below) always filter the data on a bool entitiy so that only a subset of the database displays – not the entire database.
If anyone could point me in the right direction, I would appreciate it.
Index.cshtml @model PagedList.IPagedList<GenericWebApplication.Models.Class_EntitySet01> @using PagedList.Mvc; <link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" /> @{ ViewBag.Title = "Class_EntitySet01"; } <h2>Class_EntitySet01</h2> <p> @Html.ActionLink("Create New", "Create") </p> @using (Html.BeginForm("Index", "Class_EntitySet01", FormMethod.Get)) { <p> Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string) <input type="submit" value="Search" /> </p> } <table> <tr> <th> @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter }) </th> <th> First Name </th> <th> @Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter }) </th> <th></th> </tr> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Property_Entity01) </td> <td> @Html.DisplayFor(modelItem => item.Property_Entity02) </td> <td> @Html.DisplayFor(modelItem => item.Property_Entity21) </td> <td> @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | @Html.ActionLink("Details", "Details", new { id=item.ID }) | @Html.ActionLink("Delete", "Delete", new { id=item.ID }) </td> </tr> } </table> <br /> Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount @Html.PagedListPager(Model, page => Url.Action("Index", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
Class_EntitySet01Controller.cs 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 GenericWebApplication.Models; using GenericWebApplication.DAL; using PagedList; using System.Data.Entity.Infrastructure; namespace GenericWebApplication.Controllers { public class Class_EntitySet01Controller : Controller { private GenericWebApplicationContext db = new GenericWebApplicationContext(); // GET: /Class_EntitySet01/ public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page) { ViewBag.CurrentSort = sortOrder; ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date"; if (searchString != null) { page = 1; } else { searchString = currentFilter; } ViewBag.CurrentFilter = searchString; var class_entityset01s = from s in db.Class_EntitySet01s select s; if (!String.IsNullOrEmpty(searchString)) { class_entityset01s = class_entityset01s.Where(s => s.Property_Entity01.ToUpper().Contains(searchString.ToUpper()) || s.Property_Entity02.ToUpper().Contains(searchString.ToUpper())); } switch (sortOrder) { case "name_desc": class_entityset01s = class_entityset01s.OrderByDescending(s => s.Property_Entity01); break; case "Date": class_entityset01s = class_entityset01s.OrderBy(s => s.Property_Entity21); break; case "date_desc": class_entityset01s = class_entityset01s.OrderByDescending(s => s.Property_Entity21); break; default: class_entityset01s = class_entityset01s.OrderBy(s => s.Property_Entity01); break; } int pageSize = 3; int pageNumber = (page ?? 1); return View(class_entityset01s.ToPagedList(pageNumber, pageSize)); } // GET: /Class_EntitySet01/Details/5 public ActionResult Details(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Class_EntitySet01 class_entityset01 = db.Class_EntitySet01s.Find(id); if (class_entityset01 == null) { return HttpNotFound(); } return View(class_entityset01); } // GET: /Class_EntitySet01/Create public ActionResult Create() { return View(); } // POST: /Class_EntitySet01/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 = "Property_Entity01,Property_Entity02,Property_Entity21")] Class_EntitySet01 class_entityset01) { try { if (ModelState.IsValid) { db.Class_EntitySet01s.Add(class_entityset01); db.SaveChanges(); return RedirectToAction("Index"); } } catch (RetryLimitExceededException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } return View(class_entityset01); } // GET: /Class_EntitySet01/Edit/5 public ActionResult Edit(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Class_EntitySet01 class_entityset01 = db.Class_EntitySet01s.Find(id); if (class_entityset01 == null) { return HttpNotFound(); } return View(class_entityset01); } // POST: /Class_EntitySet01/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 = "ID,Property_Entity01,Property_Entity02,Property_Entity21")] Class_EntitySet01 class_entityset01) { try { if (ModelState.IsValid) { db.Entry(class_entityset01).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } } catch (RetryLimitExceededException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } return View(class_entityset01); } // GET: /Class_EntitySet01/Delete/5 public ActionResult Delete(int? id, bool? saveChangesError = false) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } if (saveChangesError.GetValueOrDefault()) { ViewBag.ErrorMessage = "Delete failed. Try again, and if the problem persists see your system administrator."; } Class_EntitySet01 class_entityset01 = db.Class_EntitySet01s.Find(id); if (class_entityset01 == null) { return HttpNotFound(); } return View(class_entityset01); } // POST: /Class_EntitySet01/Delete/5 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Delete(int id) { try { Class_EntitySet01 class_entityset01 = db.Class_EntitySet01s.Find(id); db.Class_EntitySet01s.Remove(class_entityset01); db.SaveChanges(); } catch (RetryLimitExceededException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. return RedirectToAction("Delete", new { id = id, saveChangesError = true }); } return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { db.Dispose(); base.Dispose(disposing); } } }
__________________________________________________________________
Programming Environment:
Visual Studio 2013
.NET 4.5
Entity Framework 6 (EntityFramework 6.1.0 NuGet package)
OS: Windows 7
Project Based On:
Getting Started with Entity Framework 6 Code First using MVC 5
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
var class_entityset01s = from s in db.Class_EntitySet01s where s.SomeField == true select s;
Thank you SO much!