在 Razor 视图上的 .Include 上排序不起作用

问题描述 投票:0回答:2

我以前做过基本的排序,没有遇到任何问题,但是这个让我困惑于我做错了什么。它返回的只是一个空白页。

Details.cs(显示排序代码)

using RabiesClinics.Data;
using RabiesClinics.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;

namespace RabiesClinics.Pages.Pets;

[BindProperties]

public class DetailsModel : PageModel
{
    private readonly ApplicationDbContext _db;

    public Pet Pet { get; set; }
    public Vaccine Vaccine { get; set; }
    public IEnumerable<Vaccine> VaxList { get; set; }

    public string VaxDateSort { get; set; }

    public DetailsModel(ApplicationDbContext db)
    {
        _db = db;
    }

    public async Task<IActionResult> OnGetAsync(int ownerid, string id, string sortOrder)
    {
        var ownerid1 = ownerid;

        Pet = _db.Pet.Include(pet => pet.Vaccines).FirstOrDefault(pet => pet.PetId == id);

        VaxDateSort = sortOrder == "VaxDate_Asc_Sort" ? "VaxDate_Desc_Sort" : "VaxDate_Asc_Sort";

        IQueryable<Model.Vaccine> vaccine = from s in _db.Vaccine select s;

        switch (sortOrder)
        {
            case "VaxDate_Asc_Sort":
                vaccine = vaccine.OrderBy(s => s.DateVaccinated.ToString());
                break;
            case "VaxDate_Desc_Sort":
                vaccine = vaccine.OrderByDescending(s => s.DateVaccinated.ToString());
                break;
            default:
                vaccine = vaccine.OrderBy(s => s.DateVaccinated.ToString());
                break;
        }
        Vaccine = await vaccine.ToListAsync();

        if (Pet is null)
            return new NotFoundResult();
        else
            return Page();
    }

}

进行排序的宠物详细信息视图(因空格而缩短):

@page
@using RabiesClinics.Model
@model RabiesClinics.Pages.Pets.DetailsModel

<form method="post">
    <div class="border p-3 mt-4">
        <div class="row pb-2">
            <h2 class="text-primary pl-3">Pet Details <img src="~/Images/PawPrints.png" /></h2>
            <hr />
        </div>
        <table class="table table-borderless" style="width:100%">
            <tr>
                <td style="width: 20%">
                    <div class="mb-3">
                        <label asp-for="Pet.OwnerId"></label>
                        <input asp-for="Pet.OwnerId" name="ownerid" disabled class="form-control" />
                    </div>
                </td>
                <td style="width: 20%">
                    <div class="mb-3">
                        <label asp-for="Pet.PetId"></label>
                        <input asp-for="Pet.PetId" name="id" disabled class="form-control" />
                    </div>
                </td>
                <td style="width: 20%">
                    <div class="mb-3">
                        <label asp-for="Pet.PetName"></label>
                        <input asp-for="Pet.PetName" type="text" disabled class="form-control"/>
                    </div>
                </td>
                <td style="width: 20%">
                    <div class="mb-3">
                        <label asp-for="Pet.LastVaxDate"></label>
                        <input asp-for="Pet.LastVaxDate" type="text" disabled class="form-control" />
                    </div>
                </td>
                <td style="width: 20%">
                    <div class="form-group, mb-3">
                        <label asp-for="Pet.Status"></label>
                        <br />
                        @foreach (var item1 in Html.GetEnumSelectList<Status>())
                        {
                            <input type="radio" disabled asp-for="Pet.Status" value="@item1.Text" />
                        @item1.Text
                        }
                    </div>
                </td>
            </tr>
        </table>
        <hr />
        <h4>Vaccines</h4>
        <br />
        <table class="table table-bordered table-striped width:100%">
            <thead>
                <tr>
                    <th>
                        Tag No
                    </th>
                    <th>
                        <a asp-page="./Details" asp-route-sortOrder="@Model.VaxDateSort">Date Vaxxed</a>
                    </th>
                    <th>
                        Print Rabies Certificate
                    </th>
                </tr>
            </thead>
            <tbody>
                @for (var i = 0; i < Model.Pet.Vaccines.Count; i++)
                {
                    <tr>
                        <td style="width: 10%">
                            <div class="mb-3">
                                <input asp-for="Pet.Vaccines[i].RabiesTagNo" type="text" readonly class="form-control-plaintext" />
                            </div>
                        </td>
                        <td style="width: 10%">
                            <div class="mb-3">
                                <input asp-for="Pet.Vaccines[i].DateVaccinated"  type="date" readonly class="form-control-plaintext" />
                            </div>
                        </td>
                        <td width="10%">
                            <div class="w-75 btn-group" role="group">
                                <!--Line below is good.-->
                                <a asp-page="/RabiesCertificates/Details" class="btn btn-primary mx-2" asp-route-id="@Model.Pet.PetId" asp-route-vaxdate="@Model.Pet.Vaccines[i].DateVaccinated">
                                    <i class="bi bi-printer"></i>
                                </a>
                            </div>
                        </td>
                    </tr>
                }
        </table>
        <div>
            <a asp-page="/Owners/Details" asp-route-ownerid="ownerid1" asp-route-id="@Model.Pet.OwnerId"  class="btn btn-primary" style="width: 250px;">Back to Owner Details</a>
        </div>
    </div>
</form>

@section Scripts{
    <partial name="_ValidationScriptsPartial"/>
}

如果有人能指出我误入歧途的地方,我将不胜感激!到目前为止,我从这个论坛学到了很多东西,我已经能够在多个应用程序上申请了!

添加:根据建议更新

public async Task<IActionResult> OnGetAsync(int ownerid, string id, string sortOrder, DateTime vax)
    {
        var ownerid1 = ownerid;

        Pet = _db.Pet.Include(pet => pet.Vaccines).FirstOrDefault(pet => pet.PetId == id);

        VaxDateSort = sortOrder == "VaxDate_Asc_Sort" ? "VaxDate_Desc_Sort" : "VaxDate_Asc_Sort";

        IQueryable<Model.Vaccine> vaccine = from s in _db.Vaccine select s;

        switch (sortOrder)
        {
            case "VaxDate_Asc_Sort":
                vaccine = vax.OrderBy(s => s.DateVaccinated);
                break;
            case "VaxDate_Desc_Sort":
                vaccine = vax.OrderByDescending(s => s.DateVaccinated);
                break;
            default:
                vaccine = vax.OrderBy(s => s.DateVaccinated);
                break;
        }
        VaxList = await vax.ToListAsync();
c# asp.net sorting razor-pages
2个回答
0
投票

代码应该是这样的

        IQueryable<Model.Vaccine> vaccine = from s in _db.Vaccine select s;
        var vax = null;
        switch (sortOrder)
        {
            case "VaxDate_Asc_Sort":
                vax = vaccine.OrderBy(s => s.DateVaccinated);
                break;
            case "VaxDate_Desc_Sort":
                vax = vaccine.OrderByDescending(s => s.DateVaccinated);
                break;
            default:
                vax = vaccine.OrderBy(s => s.DateVaccinated);
                break;
        }
        VaxList = await vax.ToListAsync();

0
投票

我将从一些生活质量改进开始。

您用了 14 行来描述排序:

VaxDateSort = sortOrder == "VaxDate_Asc_Sort" ? "VaxDate_Desc_Sort" : "VaxDate_Asc_Sort";

IQueryable<Model.Vaccine> vaccine = from s in _db.Vaccine select s;

switch (sortOrder)
{
    case "VaxDate_Asc_Sort":
        vaccine = vaccine.OrderBy(s => s.DateVaccinated.ToString());
        break;
    case "VaxDate_Desc_Sort":
        vaccine = vaccine.OrderByDescending(s => s.DateVaccinated.ToString());
        break;
    default:
        vaccine = vaccine.OrderBy(s => s.DateVaccinated.ToString());
        break;
}
Vaccine = await vaccine.ToListAsync();

如果您希望将来添加多种排序方法,那么保持这种状态是有道理的。然而,问题又出在细节上:

VaxDateSort = sortOrder == "VaxDate_Asc_Sort" ? "VaxDate_Desc_Sort" : "VaxDate_Asc_Sort";

为什么你要把

VaxDateSort
sortOrder
相反?如果您事后不改变它,为什么还要拥有一个单独的属性呢?

由于

VaxDateSort
暗示始终有一个值,因此您可以将
default
switch 语句添加到另一个 case,例如:

default:
case "VaxDate_Asc_Sort":
    vaccine = vaccine.OrderBy(s => s.DateVaccinated.ToString());
    break;

现在,您已经获得了一些帮助来修复其他一些错误,因此将其与您的改进版本放在一起并修复您最后犯的错误(

vax
是您方法的日期时间参数,而不是数据库列表),这至少应该让你的代码可以编译。鉴于您的代码状态,我预计会出现更多问题。我建议仔细查看您想要执行的操作的官方 MS 文档示例并交叉引用 https://learn.microsoft.com/en-us/aspnet/core/razor-pages/?view= aspnetcore-8.0&tabs=visual-studio

using RabiesClinics.Data;
using RabiesClinics.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;

namespace RabiesClinics.Pages.Pets;

[BindProperties]    
public class DetailsModel : PageModel
{
    private readonly ApplicationDbContext _db;

    public Pet Pet { get; set; }
    public Vaccine Vaccine { get; set; }
    public IList<Vaccine> VaxList { get; set; }

    public string VaxDateSort { get; set; }

    public DetailsModel(ApplicationDbContext db)
    {
        _db = db;
    }

    public async Task<IActionResult> OnGetAsync(int ownerid, string id, string sortOrder, DateTime vaxDate)
    {    
        //TODO: inccorporate ownerid and vaxDate as filters
        SelectedPet = _db.Pet
            .Include(pet => pet.Vaccines)
            .FirstOrDefault(pet => pet.PetId == id);

        var vaccines = from s in _db.Vaccine select s;

        if (sortOrder == "VaxDate_Desc_Sort")
            VaxList = await vaccines.OrderByDescending(a => a.DateVaccinated).ToListAsync();
        else
            VaxList = await vaccines.OrderBy(a => a.DateVaccinated).ToListAsync();
                    
        if (Pet is null)
            return new NotFoundResult();
        else
            //TODO: HTML view does not use the Model.Vaccine or Model.VaxList
            return Page();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.