简体   繁体   English

我如何基于MVC应用程序中的下拉选择筛选表,然后显示数据?

[英]c# - How can I filter the table based on a dropdown selection in mvc application and then display the data?

I am doing my first ever mvc application and I am kind of stuck. 我正在做我的第一个mvc应用程序,但有点卡住了。 What I'm trying to do is filter the existing table based on the selection from the dropdown list. 我正在尝试做的是基于下拉列表中的选择来过滤现有表。 I have a Course table and a Teacher table and I'd like to be able to filter the Courses by who is teaching them. 我有一个“课程”表和一个“教师”表,我希望能够由谁来教授这些课程。

This is my controller so far: 到目前为止,这是我的控制器:

namespace TTimetable.Controllers
{
    public class CoursesController : Controller
    {
        private TimetabledbEntities db = new TimetabledbEntities();

        // GET: Courses
        public ActionResult Index(int teacher)
        {
            ViewBag.Teacher = new SelectList(db.Teacher, "teacher_Id", "lastName");
            var course = db.Course.Include(c => c.Classroom).Include(c => c.Teacher);
            return View(course.ToList());
        }

This is my View: 这是我的观点:

@model IEnumerable<TTimetable.Models.Course>

@{
    ViewBag.Title = "Courses";
}

<h2>Courses</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.course_start)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.course_end)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Classroom.classroom_no)
        </th>
        <th>
            teacher
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.course_start)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.course_end)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Classroom.classroom_no)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Teacher.firstName)
        </td>
    </tr>
}

</table>

<h2>Courses taught by:</h2>
@Html.DropDownList("Teacher", "Select teacher")

So far I was only able to make the dropdown list display the teachers from the database. 到目前为止,我只能使下拉列表显示数据库中的教师。 Anyone who could help me with this? 有人可以帮助我吗? It'll be much appreciated. 将不胜感激。 Thanks 谢谢

You will need to have your dropdown list post back to the server. 您需要将下拉列表发回到服务器上。 This can be accomplished by wrapping the dropdown in a form, and adding javascript to submit the form: 这可以通过将下拉列表包装在表单中并添加javascript提交表单来完成:

using (Html.BeginForm("Index", "Courses", FormMethod.Get))
{
    Html.DropDownList("Teacher", ViewBag.Teacher, "Select teacher", new { onchange = @"form.submit();" });
}

here viewbag should be casted as it is dynamic type object. 在这里,应将viewbag强制转换为动态类型的对象。

@Html.DropDownList("Teacher", (SelectList)ViewBag.Teacher, "Select teacher", new { onchange = @"form.submit();" });

hope it helps for your solution. 希望它对您的解决方案有所帮助。

Suggest you to use to Odata approach , when you have multiple filters , for a single table . 当您有多个过滤器时,建议您对单个表使用Odata方法。 It will be easy for you in the future. 将来对您来说会很容易。

http://www.c-sharpcorner.com/UploadFile/4d9083/creating-simple-cascading-dropdownlist-in-mvc-4-using-razor/ http://www.c-sharpcorner.com/UploadFile/4d9083/creating-simple-cascading-dropdownlist-in-mvc-4-using-razor/

combining both the techniques can help you. 结合这两种技术可以为您提供帮助。

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

相关问题 如何根据 MVC 中的选择下拉列表在表格上显示特定项目? - How can I display specific items on a table based on dropdownlistfor selection in MVC? 在同一个视图中根据下拉选择MVC C# 将列表返回到表格形式 - Return a list into a table form based on dropdown selection MVC C# in the same view 基于下拉选择 mvc 4 razor c# 显示/隐藏控件 - Show/hide control based on dropdown selection mvc 4 razor c# 如何基于Gridview本身上存在的下拉选择显示Gridview数据? - How to display Gridview data based on Dropdown selection that exists on the Gridview itself? 我如何在ASP.NET C#MVC3中将数据从数据表导出到Excel? - How can i export the data from a data table to excel in asp.net c# mvc3? 基于下拉选择填充C#Listview C# - Populate Listview based of dropdown selection c# 如何根据条件在 MVC VIEW 中显示数据? - How can I display data in the MVC VIEW based on condition? 如何根据下拉选择过滤FileDialog? - How can I filter a FileDialog depending on a dropdown selection? 如何在MVC C#应用程序上将php作为帖子执行? - How can I execute php as an post on a MVC C# application? 我如何 select 并将数据行保存在表格中? C# ASP.NET MVC 5 - How can I select and save data row in a table? C# ASP.NET MVC 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM