ASP.NET MVC - Implementing Search Functionality

Malam Malam 226 Reputation points
2024-06-06T04:48:06.3933333+00:00

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);

}

}


ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,393 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 28,276 Reputation points Microsoft Vendor
    2024-06-06T06:16:16.51+00:00

    Hi @Malam Malam,

    You wrote wrong parameters in Html.BeginForm, and you need to change button type to submit.

    User's image

    @using (Html.BeginForm("SearchAct", "Students", FormMethod.Post))
    {
        <div>
            Last Name:<br>
            <input type="text" id="nameToFind" name="nameToFind">      
            <input type="submit" id="submitId" value="submit" />
        </div>
    }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful