使用下拉列表将我的动物分类

Use the drop down list to divide my animals into categories

本人是aspnetcore mvc技术的程序员, 我在数据库中有属于类别的动物, 我想使用 select 中的下拉列表按类别对它们进行排序 像这样:enter image description here

我尝试这样做但没有成功,所以我会 post 我尝试做的事情并且很乐意提供帮助。

我的操作:

public async Task<IActionResult> Index(int id)
    {
        var petShopDbContext = 
            _context.Animals.Include(a => a.Category).Where(c=> c.Category.CategoryId == id);
        ViewBag.CategoryId = new SelectList(_context.Categories, "CategoryId", "Name");
        return View(await petShopDbContext.ToListAsync());
    }

select 在视图中:

<td><select asp-items="ViewBag.CategoryId" onchange="location.href=this.value"></select></td>

查看: @model IEnumerable

<table class="table">
<thead>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.BirthDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PhotoUrl)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Category.Name)
        </th>
        <th>@Html.DisplayNameFor(model => model.AnimalId)</th>
    </tr>
</thead>
<tbody>
@foreach (var item in Model) {
    <tr>

        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BirthDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PhotoUrl)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Category.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.AnimalId)
        </td>

    </tr>

这是您可以检查的完整工作演示:

型号:

public class Animal
{
    public Animal()
    {
        Comments = new HashSet<Comment>();
        //Category = new Category();
    }
    [Key]
    public int AnimalId { get; set; }
    [StringLength(50)]
    public string? Name { get; set; }
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    [DataType(DataType.Date)]
    [Display(Name = "Birth Date")]
    public DateTime? BirthDate { get; set; }
    public string? Description { get; set; }
    public int CategoryId { get; set; }
    [StringLength(200)]
    [Display(Name = "Portrait")]
    public string? PhotoUrl { get; set; } 

    [ForeignKey("CategoryId")]
    public virtual Category? Category { get; set; } 
    public virtual ICollection<Comment> Comments { get; set; }

}
public class Category
{
    public int CategoryId { get; set; }
    public string Name { get; set; } = null!;
}

视图(Index.cshtml):

@model IEnumerable<Animal>               //I change the href here...
<select asp-items="ViewBag.CategoryId" onchange="location.href='/Home/Index/'+this.value"></select>

<table class="table">
<thead>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.BirthDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.PhotoUrl)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Category.Name)
        </th>
        <th>@Html.DisplayNameFor(model => model.AnimalId)</th>
    </tr>
</thead>
<tbody>
@foreach (var item in Model) {
    <tr>

        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BirthDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PhotoUrl)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Category.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.AnimalId)
        </td>

    </tr>
    }
</table>

控制器:

我修改了您的代码:ViewBag.CategoryId = new SelectList(_context.Categories, "CategoryId", "Name",id); 用于维护 selected 值。这不会影响您的结果。

public class HomeController : Controller
{
    private readonly MvcProj6_0Context _context;
    
    public HomeController(MvcProj6_0Context context)
    {
        _context = context;
    }
    public async Task<IActionResult> Index(int id)
    {
        var petShopDbContext =
            _context.Animal.Include(a => a.Category).Where(c => c.Category.CategoryId == id);
        ViewBag.CategoryId = new SelectList(_context.Categories, "CategoryId",  "Name",id);
        return View(await petShopDbContext.ToListAsync());
    }

}

结果:

当您首次呈现索引视图时,id 为0 且where 子句无法在数据库中找到具有id=0 的任何记录。只有当你 select 下拉列表并传递特定的 id 时,它才会呈现相应的记录。