ASP.NET MVC:下拉列表显示所有内容,也单独显示
我目前单独显示每个类别,但我想使所有类别一起查看它们。
在屏幕截图中,我分别显示每个类别我所有类别中的所有动物
我的存储库:
public async Task<IEnumerable<Animal>> SelectAnimalsById(int id)
{
var animal = _context.Animals
.Include(a => a.Category)
.Where(c => c.Category!.CategoryId == id);
return await animal.ToListAsync();
}
public DbSet<Category> GetCategories()
{
var category = _context.Categories;
return category;
}
我的控制器
// Displays the animals by category using the Selectlist
public async Task<IActionResult> Commands(int id = 1)
{
var petShopDbContext = await _animalRepository.SelectAnimalsById(id);
ViewBag.Category = new SelectList(_catalogRepository.GetCategories(), "CategoryId", "Name", id);
return View(petShopDbContext);
}
:我的
@model IList<PetShop.Data.Models.Animal>
<label>Please select a category:</label>
<select asp-items="ViewBag.Category" onchange="location.href='/Admin/Commands/'+this.value" id="DesignSelect"></select>
<table id="Table">
<thead>
<tr>
<th><label asp-for="@Model[0].PhotoUrl"></label></th>
<th><label asp-for="@Model[0].Name"></label></th>
<th><label asp-for="@Model[0].Category.Name"></label></th>
<th><label asp-for="@Model[0].Description"></label></th>
<th><label asp-for="@Model[0].BirthDate"></label></th>
<th>Edit Animal</th>
<th>Delete Animal</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model!) {
<tr>
<td><img src="~/Images/@item.PhotoUrl" id="ManagerImage"></td>
<td>@item.Name</td>
<td>@item.Category!.Name</td>
<td>@item.Description</td>
<td>@Html.DisplayFor(model => item.BirthDate)</td>
<td><a asp-action="EditAnimal" asp-route-id="@item.AnimalId"><input type="submit" value="Edit" id="DesignButton"></a></td>
<td><a asp-action="DeleteAnimal" asp-route-id="@item.AnimalId"><input type="submit" value="Delete" id="DesignButton"></a></td>
</tr>
}
</tbody>
</table>
I currently display each category individually, but I want to make it possible to view them all together.
Here in the screenshot, I show each category separately, but I want to add another All option that will show me all the animals in all the categories
My repository:
public async Task<IEnumerable<Animal>> SelectAnimalsById(int id)
{
var animal = _context.Animals
.Include(a => a.Category)
.Where(c => c.Category!.CategoryId == id);
return await animal.ToListAsync();
}
public DbSet<Category> GetCategories()
{
var category = _context.Categories;
return category;
}
My controller:
// Displays the animals by category using the Selectlist
public async Task<IActionResult> Commands(int id = 1)
{
var petShopDbContext = await _animalRepository.SelectAnimalsById(id);
ViewBag.Category = new SelectList(_catalogRepository.GetCategories(), "CategoryId", "Name", id);
return View(petShopDbContext);
}
My view:
@model IList<PetShop.Data.Models.Animal>
<label>Please select a category:</label>
<select asp-items="ViewBag.Category" onchange="location.href='/Admin/Commands/'+this.value" id="DesignSelect"></select>
<table id="Table">
<thead>
<tr>
<th><label asp-for="@Model[0].PhotoUrl"></label></th>
<th><label asp-for="@Model[0].Name"></label></th>
<th><label asp-for="@Model[0].Category.Name"></label></th>
<th><label asp-for="@Model[0].Description"></label></th>
<th><label asp-for="@Model[0].BirthDate"></label></th>
<th>Edit Animal</th>
<th>Delete Animal</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model!) {
<tr>
<td><img src="~/Images/@item.PhotoUrl" id="ManagerImage"></td>
<td>@item.Name</td>
<td>@item.Category!.Name</td>
<td>@item.Description</td>
<td>@Html.DisplayFor(model => item.BirthDate)</td>
<td><a asp-action="EditAnimal" asp-route-id="@item.AnimalId"><input type="submit" value="Edit" id="DesignButton"></a></td>
<td><a asp-action="DeleteAnimal" asp-route-id="@item.AnimalId"><input type="submit" value="Delete" id="DesignButton"></a></td>
</tr>
}
</tbody>
</table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种方法是您可以在存储库中更改代码如下:
另一种方式是您可以更改 action 如下所示:
然后将您的视图更改为以下:
One way is that you can change your code in repository like below:
Another way is that you can change your action code like below:
Then change your view like below:
您可以在存储库中的有条件添加Where子句...
You could conditional add the where clause in your repository...