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

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

user9385
1#
user9385 Published in September 21, 2018, 8:04 am

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" />
}
share|improve this question
  • You need to bind the model to the view – Ryan Wilson Feb 13 at 20:50
  • What HTML is generated by that Razor @Rawan? Please include that in your post. – mjwills Feb 13 at 20:53
  • What HTML is generated by that Razor? I'm only write Razor and controller code – Rawan Feb 13 at 20:57
  • 1
    searching is null. Make it not null, or remove that part of the query. – Sam Axe Feb 13 at 21:16
  • 1
    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. – Stephen Muecke Feb 13 at 21:25

1 Answer 1

active oldest votes
up vote 0 down vote
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO