我在这里检查了类似的问题,但找不到任何适用于我的问题的问题。
我正在尝试一些基于ASP.NET Core MVC文档中的教程的代码。
我有一个表显示了数据库中的学生列表,其中有两列:FirstName
和LastName
。
使用任一列使用 LastName唯一的问题是,我不知道如何在提交搜索后继续在 我的视图和控制器如下: 家庭控制器>索引:<input type="text">
进行搜索效果良好。我只需要在<select>
中选择FirstName或<select>
中显示所选的选项。@model IEnumerable<Student>
<h1>Students list</h1>
<form asp-action="Index" method="get">
<div>
<p>
Filter by
<select name="chosenFilter">
<option value="firstName">@Html.DisplayNameFor(model => model.FirstName)</option>
<option value="lastName">@Html.DisplayNameFor(model => model.LastName)</option>
</select>
<input type="text" name="searchString" value="@ViewData["SearchString"]" />
<input type="submit" value="Search" />
</p>
</div>
</form>
<table class="table">
<thead>
<tr>
<th>@Html.DisplayNameFor(model => model.FirstName)</th>
<th>@Html.DisplayNameFor(model => model.LastName)</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.FirstName)</td>
<td>@Html.DisplayFor(modelItem => item.LastName)</td>
</tr>
}
</tbody>
</table>
public IActionResult Index(string searchString, string chosenFilter)
{
IQueryable<Student> students = _context.Student;
if (!String.IsNullOrEmpty(searchString))
{
switch (chosenFilter)
{
case "firstName":
students = students.Where(s => s.FirstName.Contains(searchString));
break;
case "lastName":
students = students.Where(s => s.LastName.Contains(searchString));
break;
}
}
ViewData["SearchString"] = searchString;
return View(students);
}
我自己得到的!所以我在这里分享。也许它不是最佳的,但它对我有效。
在检查并尝试";选择标签助手";文档
(参见:此链接)
我发现了它是如何工作的,并在代码中实现了它。
该解决方案不在视图中列出选择选项,而是在ViewModel中列出。因此,select标记助手可以使用它为select选项生成html,根据控制器中的条件选择您想要选择的选项。
(实际上,我也借此机会将我的学生列表和searchString添加到ViewModel中)。遵循新代码。
视图模型:
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Collections.Generic;
namespace TestApplication.Models.ViewModels
{
public class StudentViewModel
{
public IEnumerable<Student> Students { get; set; }
public string SearchString { get; set; }
public string Filter { get; set; }
public List<SelectListItem> Columns { get; } = new List<SelectListItem>
{
new SelectListItem { Value = "firstName", Text = "First Name"},
new SelectListItem { Value = "lastName", Text = "Last Name"},
};
}
}
索引操作:
public IActionResult Index(string searchString, string chosenFilter)
{
var viewModel = new StudentViewModel();
IQueryable<Student> students = _context.Student;
if (!String.IsNullOrEmpty(searchString))
{
switch (chosenFilter)
{
case "firstName":
viewModel.Filter = "firstName";
students = students.Where(s => s.FirstName.Contains(searchString));
break;
case "lastName":
viewModel.Filter = "lastName";
students = students.Where(s => s.LastName.Contains(searchString));
break;
}
}
viewModel.Students = students;
//ViewData["SearchString"] = searchString;
viewModel.SearchString = searchString;
//return View(student)
return View(viewModel);
}
视图:
@model TestApplication.Models.ViewModels.StudentViewModel
@{
ViewData["Title"] = "Students";
}
<h1>Students list</h1>
<form asp-action="Index" method="get">
<div>
<p>
Filter by <select name="chosenFilter" asp-for="Filter" asp-items="Model.Columns"></select>
<input type="text" name="searchString" value="@Model.SearchString" />
<input type="submit" value="Search" />
<a asp-action="Index">Full list</a>
</p>
</div>
</form>
<table class="table">
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Students)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.FirstName)</td>
<td>@Html.DisplayFor(modelItem => item.LastName)</td>
</tr>
}
</tbody>
</table>