简体   繁体   English

如何在 ASP.NET MVC 中通过下拉列表进行搜索

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

I'm use ASP.NET MVC.我正在使用 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.我在 SQL Server 中有一个Student表,我想使用下拉列表在学生视图中执行(搜索)名称和按年级搜索。 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" />
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM