Home How to search by drop down list in ASP.NET MVC
Reply: 1

How to search by drop down list in ASP.NET MVC

Rawan
1#
Rawan Published in 2018-02-13 20:48:17Z

I'm use ASP.NET MVC. I have a Student table in SQL Server and I want to do (search by) name and search by grade in student view by using dropdown list. The problem is the search is not working - when I click on search button, it only refreshes the page with all students.

This is the student controller code

public ActionResult Index(string searching, string searchby, int? page)
{
    var student = db.Supervisors;

    if(searchby == "Name")
        return View(student.Where(x => x.name.Contains(searching) || searching == null).ToList().ToPagedList(page ?? 1, 8));
    else
        return View(student.Where(x => x.grade.Contains(searching) || searching == null).ToList().ToPagedList(page ?? 1, 8));
}

This is the razor code

@using (Html.BeginForm("Index", "student",FormMethod.Get))
{
        @Html.DropDownList("Searchby", new SelectList(new[] { "Name", "Grade" }))
        @Html.TextBox("search")
        <input type="submit" value="Search" />
}
Rawan
2#
Rawan Reply to 2018-02-14 04:47:32Z

There is nothing in your form that submits a value that binds to your searching paramater so its always null. Your textbox is named search, not searching

just i changed search to searching

    @Html.TextBox("searching")
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.379593 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO