[英]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.