CuriousLife
CuriousLife

Reputation: 11

How to Create Search Bar in ASP.Net Core, if we have our list view using StudentCourseViewModel

I have Student Table from database contains Student Roll No, Name, Mother Name, Class, Age, Fee Paid, CourseID(fk), IsActive

in Course Table from database contains courseid(pk) and Course Name, IsActive

Add New Student ... Course Comes from Dropdown menu using AJAX,

How To Make Search Bar on top of StudentList where we can search on the basis of Fees, Course Name, Student Name, Age, MotherName? What Will Be Our Steps to create view and async method in controller, and model if required. in ASP.Net Core Entity FrameWork

Code in ASP.Net Core for views, controller and model if required or change in model

Upvotes: -1

Views: 476

Answers (1)

Rena
Rena

Reputation: 36715

For how to make search bar on the top of the student list with several conditions, here is a working demo you could follow:

Model

public class Student
{
    public int Id { get; set; }
    public string RollNo { get; set; }
    public string Name { get; set; }
    public string MotherName { get; set; }
    public string Class { get; set; }
    public int Age { get; set; }
    public string FeePaid { get; set; }
    public bool IsActive { get; set; }
    public int CourseID { get; set; }
    public Course Course { get; set; }
}
public class Course
{
    public int CourseID { get; set; }
    public string CourseName { get; set; }
    public bool IsActive { get; set; }
    public List<Student> Students { get; set; }
}

View(Index.cshtml)

@model IEnumerable<Student>
<h1>Index</h1>

<p>
    <a asp-action="Create">Create New</a>
</p>
<h1>Search</h1>
<form method="post">
    <select name="SearchType" >
        <option value="Name">Name</option>
        <option value="MotherName">MotherName</option>
        <option value="FeePaid">FeePaid</option>
        <option value="Age">Age</option>
        <option value="CourseName">CourseName</option>               
    </select>
    <input type="text" name="SearchString" placeholder="enter the search value"/>
    <input type="submit" value="Search"/>
</form>


<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.RollNo)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.MotherName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Class)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Age)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.FeePaid)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.IsActive)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Course.CourseName)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.RollNo)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.MotherName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Class)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Age)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FeePaid)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.IsActive)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Course.CourseName)
            </td>
            <td>
                <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
                <a asp-action="Details" asp-route-id="@item.Id">Details</a> |
                <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
            </td>
        </tr>
}
    </tbody>
</table>

Controller

public class StudentsController : Controller
{
    private readonly MvcDbContext _context;

    public StudentsController(MvcDbContext context)
    {
        _context = context;
    }

    // GET: Students
    public async Task<IActionResult> Index()
    {
        return View(await _context.Student.Include(a=>a.Course).ToListAsync());
    }
    [HttpPost]
    public IActionResult Index(string SearchString,string SearchType)
    {
        var query = _context.Student.Include(a => a.Course);
        var model = new List<Student>();
        switch (SearchType)
        {
            case "Name":
                model = query.Where(a => a.Name == SearchString).ToList();
                break;
            case "MotherName":
                model = query.Where(a => a.MotherName == SearchString).ToList();
                break;
            case "FeePaid":
                model = query.Where(a => a.FeePaid == SearchString).ToList();
                break;  
            case "Age":
                model = query.Where(a => a.Age.ToString() == SearchString).ToList();
                break;
            case "CourseName":
                model = query.Where(a => a.Course.CourseName == SearchString).ToList();
                break;
        }
        return View(model);
    }
}

DbContext

public class MvcDbContext:DbContext
{
    public MvcDbContext(DbContextOptions<MvcDbContext> options)
    : base(options)
    {
    }
    public DbSet<Student> Student { get; set; }
    public DbSet<Course> Course { get; set; }
}

Upvotes: -1

Related Questions