将数据从一个 Razor 页面传递到另一个 Razor 页面

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

我需要一些帮助。我以前没有这样做过,而且我不完全确定在哪里添加什么。基本上,我正在开发一个用于存储历史数据的应用程序。无需创建、编辑或删除。是打印宠物狂犬病证明。

我有一个宠物查看页面,其中包含宠物已接种的疫苗。每次疫苗接种旁边都有一个按钮。我希望此按钮链接到狂犬病证书页面,并使用该宠物和特定疫苗的数据填写证书(以便可以打印)。一只宠物多年来可能接种过多种疫苗。

这是我到目前为止所拥有的(我已经删除了额外的字段来压缩帖子):

我的宠物详细信息页面:

@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" 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" disabled class="form-control" />
                </div>
            </td>
            <td style="width: 40%">
                <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="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>
                        Date Vaxxed
                    </th>
                    <th>
                        Vax Expires
                    </th>
                    <th>
                        Vet Name
                    </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].DateVaccinated" type="text" readonly class="form-control-plaintext" />
                            </div>
                        </td>
                        <td style="width: 10%">
                            <div class="mb-3">
                                <input asp-for="Pet.Vaccines[i].VaccinationExpiration" type="text" readonly class="form-control-plaintext" />
                            </div>
                        </td>
                        <td style="width: 20%">
                            <div class="mb-3">
                                <input asp-for="Pet.Vaccines[i].VetName" type="text" readonly class="form-control-plaintext" />
                            </div>
                        </td>
                        <td width="10%">
                            <div class="w-75 btn-group" role="group">
                                <a asp-page="/RabiesCertificates/Details" class="btn btn-primary mx-2">
                                    <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"/>
}

这是宠物详细信息页面的 .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(string Id)
    {
        Pet = _db.Pet
            .Include(pet => pet.Vaccines).FirstOrDefault(pet => pet.PetId == Id);

        var petid1 = Pet.PetId;
        var vaxdate1 = Vaccine.DateVaccinated;

    }

}

这是狂犬病证书页面的开头:

@page
@using RabiesClinics.Model
@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>

            <table class="table table-borderless" style="width:100%">
                <tr>
                    @foreach (var obj in Model.Filter)
                    {
                    <td style="width: 20%">
                        <div class="mb-3">
                            <label asp-for="@obj.LastName"></label>
                            <input asp-for="@obj.LastName" disabled class="form-control" />
                        </div>
                    </td>
                    <td style="width: 20%">
                        <div class="mb-3">
                            <label asp-for="@obj.FirstName"></label>
                            <input asp-for="@obj.FirstName" disabled class="form-control" />
                        </div>
                    </td>
                    <td style="width: 20%">
                        <div class="mb-3">
                            <label asp-for="@obj.Telephone"></label>
                            <input asp-for="@obj.Telephone" disabled class="form-control" />
                        </div>
                    </td>
                    }
                </tr>
            </table>
        </div>
    </form>
</div>

<button type="submit" onclick="printDiv('RabiesCertificate')" class="btn btn-primary" style="width:300px;">Print Rabies Certificate</button>

@section Scripts
    {
    <style>
        tr{
            text-align:right;
        }
    </style>

    <script language="javascript">
        function printDiv(divName) {
            var printContents = document.getElementById(divName).innerHTML;
            var originalContents = document.body.innerHTML;

            document.body.innerHTML = printContents;

            window.print();

            document.body.innerHTML = originalContents;
        }
    </script>
    }

以及狂犬病证书页面的 .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<Filter> Filter { get; set; }

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

            ViewData["endingparameter"] = $"For Vaccination Date: {vaxdate1} and Pet Id: {petid1}";

            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 Filter
                            {
                                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();
        }

    }
}

我在这里查看了其他几篇文章,但不太了解回复/答案,不知道在我自己的代码中放置什么。

谢谢!!

asp.net-core variables parameter-passing viewmodel razor-pages
1个回答
0
投票

您可以在宠物详细信息中使用 ViewData["petid1"].cs:

 ViewData["petid1"] = Pet.PetId;

然后尝试使用以下代码

<a>

<a id="atag" 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>
© www.soinside.com 2019 - 2024. All rights reserved.