我有以下 .NET Razor 页面,无论我尝试什么,似乎都没有绑定任何属性,POST
并且它们都是空的。
当我查看发布的对象时,所有属性均为空。
包含标签助手
@page
@model AppointmentModel
@{
ViewBag.Title = "Rendez-Vous";
ViewBag.pTitle = "Rendez-Vous";
ViewBag.pageTitle = "Accueil";
Layout = "~/Views/Shared/_Layout.cshtml";
}
...
<form id="formSubmit" method="post">
<div class="row">
<div class="col-md-6">
<div class="mb-3">
<label for="formrow-firstname-input" class="form-label">Prénom</label>
<input asp-for="Input.FirstName" type="text" class="form-control" id="formrow-firstname-input" placeholder="Entrez votre prénom" autocomplete="firstname" disabled>
</div>
</div>
<div class="col-md-6">
<div class="mb-3">
<label for="formrow-lastname-input" class="form-label">Nom de Famille</label>
<input asp-for="Input.LastName" class="form-control" autocomplete="lastname" aria-required="true" placeholder="Entrez votre nom de Famille" disabled />
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="mb-3">
<label for="formrow-specialite" class="form-label">Cliniques</label>
<select id="formrow-specialite" asp-for="Input.Clinic" asp-items="Model.Locations" class="form-select"></select>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="mb-3">
<label>Date désirée du rendez-vous</label>
<div class="input-group" id="datepicker2">
<input type="text" class="form-control" placeholder="dd M, yyyy"
data-date-format="dd M, yyyy" data-date-container='#datepicker2' data-provide="datepicker"
data-date-autoclose="true">
<span class="input-group-text"><i class="mdi mdi-calendar"></i></span>
</div><!-- input-group -->
</div>
</div>
<div class="col-md-6">
<div class="mb-3">
<label for="formrow-periode" class="form-label">Période de la journée</label>
<select id="formrow-periode" class="form-select">
<option>Matin</option>
<option>Après-Midi</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="mb-3">
<label for="formrow-email-input" class="form-label">Courriel</label>
<input asp-for="Input.Email" type="email" class="form-control" id="formrow-email-input" placeholder="Courriel" disabled>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="mb-3">
<label for="formrow-email-input" class="form-label">Raison de votre visite</label>
<textarea class="form-control" id="formrow-email-input" placeholder="Riason de votre visite"></textarea>
</div>
</div>
</div>
<div>
<button type="submit" id="btnSubmit" class="btn btn-primary w-md">Soumettre</button>
...
</div>
}
我尝试创建一个单独的类,几乎花了一个小时进行调试。我尝试将其放入身体中,但没有效果。我使用的是.NET 7。
这是我的班级
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Mvc;
#nullable disable
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.ComponentModel.DataAnnotations;
namespace CBQAdmin.Pages
{
[Authorize]
public class AppointmentModel : PageModel
{
private readonly CBQAdminIContext _context;
private readonly UserManager<CBQIdentityUser> _userManager;
private readonly ILogger<AppointmentModel> _logger;
[BindProperty]
public InputModel Input { get; set; }
public SelectList Locations { get; set; }
public AppointmentModel(
UserManager<CBQIdentityUser> userManager,
ILogger<AppointmentModel> logger,
CBQAdminIContext context)
{
_userManager = userManager;
_logger = logger;
_context = context;
}
public class InputModel
{
[DataType(DataType.Text)]
public string FirstName { get; set; }
[DataType(DataType.Text)]
public string LastName { get; set; }
[DataType(DataType.Text)]
public string Email { get; set; }
[DataType(DataType.Text)]
public string Clinic { get; set; }
[DataType(DataType.Text)]
public string PolicyNumber { get; set; }
[DataType(DataType.Text)]
public string Reason { get; set; }
}
public async Task OnGetAsync()
{
Locations = new SelectList(_context.Clinics.OrderBy(n => n.Name).ToList(), "Name", "Name");
var user = await _userManager.GetUserAsync(User);
Input = new InputModel();
if (user != null)
{
Input.Email = await _userManager.GetEmailAsync(user);
if (!string.IsNullOrEmpty(user.FirstName))
{
Input.FirstName = user.FirstName;
}
if (!string.IsNullOrEmpty(user.LastName))
{
Input.LastName = user.LastName;
}
if (!string.IsNullOrEmpty(user.PolicyNumber))
{
Input.PolicyNumber = user.PolicyNumber;
}
}
}
public async Task<IActionResult> OnPostAsync()
{
var user = await _userManager.GetUserAsync(User);
InputModel input = new InputModel();
await TryUpdateModelAsync<InputModel>(
input, "input", i => i.FirstName, i => i.LastName, i => i.PolicyNumber, i => i.Clinic, i => i.Reason);
if (ModelState.IsValid)
{
if (user != null)
{
EmailService emailService = new EmailService();
await emailService.SendAppointmentRequest(Input);
}
}
return Page();
}
}
}
默认情况下,浏览器不支持通过表单提交禁用属性:
对于您的场景,您可以更改
disabled
为readonly
.