在 C# Razor Pages 的帮助下,我尝试了几个小时将表作为 PartialView 返回。 首先我得到错误返回模型不等于预期模型。 我更改了视图模型,错误消失了。 现在我得到了错误:
InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'System.Collections.Generic.List`1[DantronikOrganizer.Data.Models.UserVacationEntity]'. There should only be one applicable constructor.
我从头说起: 我创建了一个索引页面来向用户显示表中的休假请求,但如果他们选择从表中删除一个条目,整个页面将被重新加载。所以我虽然为什么不使用 ajax 来解决这个问题。
我删除了索引页中的表格并在 _TableView 中创建了一个新表格。 现在,ajax 请求应将 _TableView 中的视图加载到我的索引页面中。 现在,当用户从表中删除一个条目时,只会重新加载表。 感谢新的 PartialView。
我就我的问题向 ChatGPT 询问了很多,但即使是 ChatGPT 也不明白为什么我会收到这个错误。
@page
@model DantronikOrganizer.Pages.Vacation.IndexModel
@{
ViewData["Title"] = "Urlaubsanträge";
}
<h1>Übersicht - Urlaubsanträge</h1>
<hr />
<p>
<a class="btn btn-success" asp-page="VacationRequest">Urlaub beantragen</a>
</p>
<form>
<div class="input-group mb-2 w-25">
<input id="yearFilter" asp-for="@Model.FilterYear" class="form-control" placeholder="Nach Jahr filtern" aria-label="Filer by Year" aria-describedby="btnFilter">
<button class="btn btn-outline-secondary" type="submit" id="btnFilter">Filtern</button>
</div>
<span asp-validation-for="@Model.FilterYear" class="text-danger"></span>
<div class="form-check mb-2">
<input id="approvedFilter" asp-for="@Model.FilterIsApproved" class="form-check-input" />
<label class="form-check-label" asp-for="@Model.FilterIsApproved"></label>
</div>
</form>
<hr />
<div class="row">
<div class="col-lg-6">
<h6>Ausgewähltes Jahr: </h6>
<span class="text-primary">Urlaubstage: | Bisher genutzte: | Verfügbare: </span>
</div>
</div>
<div id="partialViewContainer"></div>
<!--Modal Window to delete an entry in the table-->
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteModalLabel">Eintrag löschen</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Möchten Sie wirklich den Eintrag löschen?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Abbrechen</button>
<button type="button" class="btn btn-primary" id="deleteButton">Löschen</button>
</div>
</div>
</div>
</div>
@section Scripts{
<script>
function showToast(message) {
Toastify({
text: message,
duration: 5000,
gravity: "top",
position: "center",
style: {
background: "#ff5722"
},
stopOnFocus: true
}).showToast();
}
$(document).ready(function () {
loadPartialView();
$('#yearFilter, #approvedFilter').change(function () {
loadPartialView();
});
});
function loadPartialView() {
var year = $('#yearFilter').val();
var isApproved = $('#approvedFilter').is(':checked') || false;
$.ajax({
url: '@Url.Page("/Vacation/_TableView", "TableView")',
data: { year: year, isApproved: isApproved },
type: 'GET',
success: function (data) {
$('#partialViewContainer').html(data);
}
});
}
function deleteVacation(id) {
var token = $('input[name="__RequestVerificationToken"]').val();
$('#deleteModal').modal('show');
$('#deleteButton').on('click', function () {
if (confirm("Möchten Sie wirklich den Eintrag löschen?")) {
$.ajax({
type: "POST",
url: "/Vacation/Delete?handler=delete",
data: { id: id, __RequestVerificationToken: token },
headers: { "RequestVerificationToken": token },
success: function (response) {
if (response.success) {
showToast(response.message);
loadPartialView();
}
},
error: function (response) {
if (response.error) {
showToast(response.message);
}
}
});
$('#deleteModal').modal('hide');
}
});
}
</script>
}
using DantronikOrganizer.Areas.Identity.Data;
using DantronikOrganizer.Data.Interfaces;
using DantronikOrganizer.Data.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DantronikOrganizer.Pages.Vacation
{
public class _TableView : PageModel
{
private readonly IUserVacation _service;
private readonly UserManager<ApplicationUser> _userManager;
public List<UserVacationEntity> UserVacationList { get; set; }
public _TableView(IUserVacation service, UserManager<ApplicationUser> userManager)
{
_userManager = userManager;
_service = service;
}
public async Task<IActionResult> OnGetTableView(int year, bool isApproved)
{
var user = await _userManager.GetUserAsync(User);
UserVacationList = await _service.GetUserVacationByUser(user.Id);
if (!string.IsNullOrEmpty(year.ToString()))
{
UserVacationList = UserVacationList.Where(u => u.DtFrom.Year == year).ToList();
}
if (isApproved)
{
UserVacationList = UserVacationList.Where(x => x.IsApproved).ToList();
}
return Partial("_TableView", UserVacationList);
}
}
}
@page
@model List<DantronikOrganizer.Data.Models.UserVacationEntity>
<table class="table table-hover">
<thead>
<tr>
<th>
@Html.DisplayNameFor(m => m[0].DtFrom)
</th>
<th>
@Html.DisplayNameFor(m => m[0].DtUntil)
</th>
<th>
@Html.DisplayNameFor(m => m[0].DaysRequested)
</th>
<th>
@Html.DisplayNameFor(m => m[0].IsApproved)
</th>
<th></th>
</tr>
</thead>
<tbody>
@if (Model != null && Model.Any())
{
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.DtFrom)</td>
<td>@Html.DisplayFor(modelItem => item.DtUntil)</td>
<td>@Html.DisplayFor(modelItem => item.DaysRequested)</td>
<td>@Html.DisplayFor(modelItem => item.IsApproved)</td>
<td>
<a class="btn btn-primary" asp-page="./Edit" asp-route-id="@item.Id">Bearbeiten</a> |
<a class="btn btn-primary" asp-page="./Details" asp-route-id="@item.Id">Details</a> |
<button class="btn btn-danger" onclick="deleteVacation(@item.Id)">Löschen</button>
</td>
</tr>
}
}
else
{
<tr>
<td colspan="5">No vacation entries found.</td>
</tr>
}
</tbody>
</table>