ASP.NET MVC:下拉列表显示所有内容,也单独显示

发布于 2025-01-21 18:09:50 字数 2789 浏览 0 评论 0原文

我目前单独显示每个类别,但我想使所有类别一起查看它们。

“

在屏幕截图中,我分别显示每个类别我所有类别中的所有动物

我的存储库:

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.

enter image description here

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>

image: enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慕巷 2025-01-28 18:09:50

一种方法是您可以在存储库中更改代码如下:

public async Task<IEnumerable<Animal>> SelectAnimalsById(int id)
{
    List<Animal> data= new List<Animal>();
    var animal = _context.Animal
                        .Include(a => a.Category);
    if (id != 0)
    {
        data= await animal.Where(c => c.Category!.CategoryId == id).ToListAsync();
    }
    else
    {
        data= await animal.ToListAsync();
    }
    return data;
}

另一种方式是您可以更改 action 如下所示:

public async Task<IActionResult> Commands(int id = 1)
{
    //add this condition
    //if id=0, return all the animals with all the categories
    if(id==0)
    {
        return View(_context.Animal.Include(a => a.Category).ToList());
    }     

    //keep the same.....   
    var petShopDbContext = await _animalRepository.SelectAnimalsById(id);;
    ViewBag.Category = new SelectList(_catalogRepository.GetCategories(), "CategoryId", "Name", id);
    return View(petShopDbContext);
}

然后将您的视图更改为以下:

<select asp-items="ViewBag.Category" onchange="location.href='/Admin/Commands/'+this.value" id="DesignSelect">
    <option value="0">ALL</option>     //add this option
</select>

One way is that you can change your code in repository like below:

public async Task<IEnumerable<Animal>> SelectAnimalsById(int id)
{
    List<Animal> data= new List<Animal>();
    var animal = _context.Animal
                        .Include(a => a.Category);
    if (id != 0)
    {
        data= await animal.Where(c => c.Category!.CategoryId == id).ToListAsync();
    }
    else
    {
        data= await animal.ToListAsync();
    }
    return data;
}

Another way is that you can change your action code like below:

public async Task<IActionResult> Commands(int id = 1)
{
    //add this condition
    //if id=0, return all the animals with all the categories
    if(id==0)
    {
        return View(_context.Animal.Include(a => a.Category).ToList());
    }     

    //keep the same.....   
    var petShopDbContext = await _animalRepository.SelectAnimalsById(id);;
    ViewBag.Category = new SelectList(_catalogRepository.GetCategories(), "CategoryId", "Name", id);
    return View(petShopDbContext);
}

Then change your view like below:

<select asp-items="ViewBag.Category" onchange="location.href='/Admin/Commands/'+this.value" id="DesignSelect">
    <option value="0">ALL</option>     //add this option
</select>
梦醒灬来后我 2025-01-28 18:09:50

您可以在存储库中的有条件添加Where子句...

var animal = _context.Animals
                         .Include(a => a.Category);

if(id != 0)
{
  animal.Where(c => c.Category!.CategoryId == id);
}

return await animal.ToListAsync();

You could conditional add the where clause in your repository...

var animal = _context.Animals
                         .Include(a => a.Category);

if(id != 0)
{
  animal.Where(c => c.Category!.CategoryId == id);
}

return await animal.ToListAsync();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文