I've implemented a search to filter out records by LastName but it doesn't work; basically it does nothing. I have Googled and tried using several code sample but not been able to use them since I get syntax errors.
How do I get the search to work?
********** Index.cs **********
@model IEnumerable<DEntry.Models.Student>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
```}
<h2>Student Administration</h2>
<p>
@Html.ActionLink("Create New", "Create")
@using (Html.BeginForm("Students", "Students", FormMethod.Post))
{
<div>
Last Name:<br>
<input type="text" id="nameToFind" name="nameToFind">
<input type="button" id="submitId" value="submit" />
</div>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.StudentID)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
</tr>
@foreach (var item in Model)
{
<td>
@Html.DisplayFor(modelItem => item.StudentID)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
}
********** Student.cs **********
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace DEntry.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
public partial class Student
{
public int Id { get; set; }
[Required]
public Nullable<int> StudentID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
********** StudentsController.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 DEntry.Models;
namespace DEntry.Controllers
{
public class StudentsController : Controller
{
private EISEntities db = new EISEntities();
// GET: Students
public ActionResult Index()
{
return View(db.Students.ToList());
}
// GET: Students/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Student Student = db.Students.Find(id);
if (Student == null)
{
return HttpNotFound();
}
return View(Student);
}
// GET: Students/Create
public ActionResult Create()
{
return View();
}
// POST: Students/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,StudentID,FirstName,LastName")] Student Student)
{
if (ModelState.IsValid)
{
db.Students.Add(Student);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(Student);
}
// GET: Students/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Student Student = db.Students.Find(id);
if (Student == null)
{
return HttpNotFound();
}
return View(Student);
}
// POST: Students/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,StudentID,FirstName,LastName")] Student Student)
{
if (ModelState.IsValid)
{
db.Entry(Student).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(Student);
}
// GET: Students/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Student Student = db.Students.Find(id);
if (Student == null)
{
return HttpNotFound();
}
return View(Student);
}
// POST: Students/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Student Student = db.Students.Find(id);
db.Students.Remove(Student);
db.SaveChanges();
return RedirectToAction("Index");
}
//Get Action for rendering view
public ActionResult SearchAct()
{
return View();
}
[HttpPost]
public ActionResult SearchAct(string nameToFind)
{
ViewBag.SearchKey = nameToFind;
return View();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}