在同一视图页面上处理多项数据

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

我正在编写的应用程序有 3 页。这是历史性的,仅查看狂犬病疫苗信息。

  1. 主人信息 - 查看拥有的宠物列表
  2. 宠物详细信息 - 查看该特定宠物接种的疫苗列表
  3. 特定宠物/疫苗组合的可打印狂犬病证书视图

我在这篇文章中发帖询问如何将数据从第 2 页传递到第 3 页(将数据从一个 Razor 页面传递到另一个 Razor 页面),很快就得到了答复,我能够正常工作。

但是当我添加信息以将数据从第 1 页传递到第 2 页时,我在

上收到错误
ViewData["petid1"] = Pet.PetId; 

从第 2 页到第 3 页传递数据的行。当我打开第 1 页并将鼠标悬停在按钮上以转到第 2 页时,正确的所有者 ID 和宠物 ID 显示在悬停中,但实际上单击按钮会引发错误在

ViewData["petid1"] = Pet.PetId;

以下是我的观点和有问题的.cs的部分:

业主观点:

<td width="20%">
    <div class="w-75 btn-group" role="group">
        <a asp-page="/Pets/Details" class="btn btn-primary mx-2" 
           asp-route-ownerid="@ViewData["ownerid1"]" 
           asp-route-ownerspetid="@Model.Owner.OwnersPets[i].PetId">
            <i class="bi bi-printer"></i>
        </a>
    </div>
</td>

Owner.cs

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

namespace RabiesClinics.Pages.Owners;

[BindProperties]

public class DetailsModel : PageModel
{
    private readonly ApplicationDbContext _db;

    public Owner Owner { get; set; }
    public Pet Pet { get; set; }

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

    public async Task OnGetAsync(int Id)
    {
        Owner = _db.Owner
            .Include(owner => owner.OwnersPets).FirstOrDefault(owner => owner.OwnerId == Id);

        ViewData["ownerid1"] = Owner.OwnerId;
    }
}

车主模型类:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace RabiesClinics.Model
{
    public class Owner
    {
        [Key]
        [Required]
        public int OwnerId { get; set; }

        [ForeignKey("OwnerId")]
        public List<Pet>? OwnersPets { get; set; }
    }
}

宠物视角:

@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="ownerspetid" 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>
        <table class="table table-borderless" style="width:100%">
        <tr>
            <td style="width: 20%">
                <div class="form-group, mb-3">
                    <label asp-for="Pet.Species"></label>
                    <br />
                    @foreach (var item2 in Html.GetEnumSelectList<Species>())
                    {
                        <input type="radio" disabled asp-for="Pet.Species" value="@item2.Text" />
                        @item2.Text
                    }
                </div>
            </td>
            <td style="width: 20%">
                <div class="form-group, mb-3">
                    <label asp-for="Pet.Gender"></label>
                <br />
                    @foreach (var item3 in Html.GetEnumSelectList<Gender>())
                    {
                        <input type="radio" disabled asp-for="Pet.Gender" value="@item3.Text" />
                        @item3.Text
                    }
                </div>
            </td>
            <td style="width: 20%">
                <div class="form-group, mb-3">
                    <label asp-for="Pet.Altered"></label>
                    <br />
                        <input asp-for="Pet.Altered" type="checkbox" disabled data-val="true" id="Pet.Altered" name="Pet.Altered" value="true" />
                        <input name="Pet.Altered" type="hidden" value="false" />
                </div>
            </td>
            <td style="width: 20%">
                <div class="form-group, mb-3">
                    <label asp-for="Pet.Age"></label>
            <br />
                    @foreach (var item4 in Html.GetEnumSelectList<Age>())
                    {
                        <input type="radio" disabled asp-for="Pet.Age" value="@item4.Text" />
                        @item4.Text
                    }
                </div>
            </td>
            <td style="width: 20%">
                <div class="mb-3">
                    <label asp-for="Pet.Colors"></label>
                    <input asp-for="Pet.Colors" type="text" disabled class="form-control" />
                </div>
            </td>
        </tr>
    </table>
    <table class="table table-borderless" style="width:60%">
        <tr>
            <td style="width: 40%">
                <div class="mb-3">
                    <label asp-for="Pet.Breed"></label>
                    <input asp-for="Pet.Breed" type="text" disabled class="form-control" />
                </div>
            </td>
            <td style="width: 20%">
                <div class="form-group, mb-3">
                    <label asp-for="Pet.Mix"></label>
                    <br />
                    <input asp-for="Pet.Mix" type="checkbox" disabled data-val="true" id="Pet.Mix" name="Pet.Mix" value="true" />
                    <input name="Pet.Mix" type="hidden" value="false" />
                </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>
                        Date Vaxxed
                    </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="text" 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-petid="@ViewData["petid1"]" asp-route-vaxdate="@Model.Pet.Vaccines[i].DateVaccinated">
                                    <i class="bi bi-printer"></i>
                                </a>
                            </div>
                        </td>
                    </tr>
                }
        </table>
        <div>
            <a asp-page="Index" class="btn btn-secondary" style="width: 150px;">Back to List</a>
        </div>
    </div>
</form>

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

Pet.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 DetailsModel(ApplicationDbContext db)
    {
        _db = db;
    }

    public void OnGet(int ownerid, string ownerspetid, string Id)
    {
        var ownerid1 = ownerid;
        var petid2 = ownerspetid;

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

        //Line below was working before adding ownerid and ownerspetid.
        ViewData["petid1"] = Pet.PetId;
    }
}

宠物模型班:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace RabiesClinics.Model
{
    public class Pet
    {
        [Key]
        public string PetId { get; set; }

        [ForeignKey("OwnerId")]
        [Display(Name = "Owner Id")]
        public int OwnerId { get; set; }
        public Owner Owner;

        public List<Vaccine>? Vaccines { get; set; }

        [Display(Name = "Pet Name")]
        public string PetName { get; set; }
    }
}

狂犬病证书查看:

@page
@model RabiesClinics.Pages.RabiesCertificates.DetailsModel

<div id="RabiesCertificateReports" class="container p-3">
    <div class="row pt-4">
        <div class="col-6">
            <h2 class="text-primary">Rabies Certificate</h2>
        </div>
    </div>

    <br /><br />

    <form asp-page="./Details" method="get">
        <div class="form-actions no-color">
            <p>
                <h5>Report Date Range</h5>
                For Pet Id: <input type="text" name="petid" value="" /> and Vaccine Date:<input type="date" name="vaxdate" value="" />
                <input type="submit" value="Filter Report" class="btn btn-primary mx-2" />
                <a asp-page="./Pets/Detail" class="btn btn-link mx-2">Return to Pet Detail</a>
            </p>
        </div>
        <div id="RabiesCertificate" class="row pt-4">
            <div class="col-25">
                <h2 class="text-primary; text-capitalize" align="center">RABIES VACCINATION CERTIFICATE</h2>
                <h3 class="text-primary; text-capitalize" align="center" style="font-style:italic">ADAPTED NASPHV FORM 51</h3>
                <h3 class="text-primary" align="center">Human Postexposure Rabies Treatment</h3>
                <h5 class="text-primary" align="center">@ViewData["endingparameter"]</h5>
            </div>

RabiesCert.cs

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

namespace RabiesClinics.Pages.RabiesCertificates
{
    public class DetailsModel : PageModel
    {
        private readonly ApplicationDbContext _db;

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

        public IEnumerable <ViewModel> Filter { get; set; }

        public async Task OnGetAsync(string petid, DateTime vaxdate)
        {
            var petid1 = petid;
            var vaxdate1 = vaxdate.ToShortDateString();

            ViewData["endingparameter"] = $"For Pet Id: {petid1} Vaccinated On: {vaxdate1}";

            Filter = await (from x in _db.Owner
                            join y in _db.Pet on x.OwnerId equals y.OwnerId
                            join z in _db.Vaccine on x.OwnerId equals z.OwnerId
                            where y.PetId == petid && z.DateVaccinated == vaxdate
                            select new ViewModel
                            {
                                OwnerId = x.OwnerId,
                                PetId = y.PetId,
                                LastName = x.LastName,
                                FirstName = x.FirstName,
                                Telephone = x.Telephone,
                                Addr1 = x.Addr1,
                                Addr2 = x.Addr2,
                                City = x.City,
                                State = x.State,
                                Zip = x.Zip,
                                Species = y.Species,
                                Gender = y.Gender,
                                Altered = y.Altered,
                                Age = y.Age,
                                Breed = y.Breed,
                                PetName = y.PetName,
                                Colors = y.Colors,
                                DateVaccinated = (DateTime)z.DateVaccinated,
                                VaccinationExpiration = (DateTime)z.VaccinationExpiration,
                                ProducerId = z.ProducerId,
                                Duration = z.Duration,
                                VaccineLotNo = z.VaccineLotNo,
                                LotExpirationDate = (DateTime)z.LotExpirationDate,
                                VetLicenseNo = z.VetLicenseNo,
                                VetName = z.VetName,
                                VetAddress = z.VetAddress,
                            }).ToListAsync();
        }
    }
}

我尝试了多种方法来传输数据,我可以看到所有者视图中每只宠物旁边的提交按钮保存了正确的所有者 ID 和宠物 ID,但我在接收端一定不能有正确的内容宠物视图或 pet.cs。

请帮助并结束我的痛苦!谢谢你

asp.net razor-pages viewdata
1个回答
0
投票

这是我对此的解释......

所有者页面:

<a asp-page="/Pets/Details" class="btn btn-primary mx-2" 
   asp-route-ownerid="@ViewData["ownerid1"]" 
   asp-route-ownerspetid="@Model.Owner.OwnersPets[i].PetId">
    <i class="bi bi-printer"></i>
</a>

在这里,您为宠物页面提供两个参数:

ownerid
ownerspetid
。现在让我们看看宠物页面是做什么的。

Pet.cs:

public void OnGet(int ownerid, string ownerspetid, string Id)
{
    var ownerid1 = ownerid;
    var petid2 = ownerspetid;

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

    //Line below was working before adding ownerid and ownerspetid.
    ViewData["petid1"] = Pet.PetId;
}

它需要三个参数。你没有给它

Id
,但你使用
Id
来获取宠物实体。这将导致
Pet
变为
null
。在出现错误的行中,您尝试访问
Pet
的属性,但它为空,您会得到一个
NullReferenceException

你真的需要三个参数吗?

ownerspetid
与您所说的
Id
PetId
有什么不同吗?如果没有,请丢弃
ownerspetid
并将其替换为所有者页面宠物按钮中的
Id
。所以这样的事情应该有效:

所有者页面:

<a asp-page="/Pets/Details" class="btn btn-primary mx-2" 
   asp-route-ownerid="@ViewData["ownerid1"]" 
   asp-route-id="@Model.Owner.OwnersPets[i].PetId">
    <i class="bi bi-printer"></i>
</a>

Pet.cs:

public IActionResult OnGet(int ownerid, string id)
{
    Pet = _db.Pet.Include(pet => pet.Vaccines).FirstOrDefault(pet => pet.PetId == id);

    if (Pet is null)
        return new NotFoundResult();

    //Now this might work, but I don’t really see why you need it.
    //In your view you can just use @Model.Pet.PetId instead.
    ViewData["petid1"] = Pet.PetId;
    
    return Page();
}
© www.soinside.com 2019 - 2024. All rights reserved.