在 Razor Pages 中绑定单选按钮组时出现问题
Problems binding a radio button group in Razor Pages
我在 ASP.NET Core 3.0 Razor 页面中使用单选按钮组时遇到一些麻烦。本质上,我想使用无线电组而不是 select,但我不知道如何将它正确绑定到我的 InputModel。
Code 用于有问题的 Razor 页面。当然还有 repo 中的其余代码。
在我的 cshtml.cs
中,我有一个像这样定义的输入模型
public class InputModel
{
[Required]
[StringLength(
CTConfig.Story.MaxTitleLength,
ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.",
MinimumLength = CTConfig.Story.MinTitleLength
)]
public string Title { get; set; }
[Required]
[StringLength(
CTConfig.Story.MaxDescriptionLength,
ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.",
MinimumLength = CTConfig.Story.MinDescriptionLength
)]
public string Description { get; set; }
[Required]
[StringLength(
CTConfig.Story.MaxHookLength,
ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.",
MinimumLength = CTConfig.Story.MinHookLength
)]
public string Hook { get; set; }
[DataType(DataType.Upload)]
[MaxFileSize(CTConfig.Story.CoverMaxWeight)]
[AllowedExtensions(new[] {".jpg", ".jpeg", ".png"})]
public IFormFile Cover { get; set; }
[Required]
public Rating Rating { get; set; }
[Required]
public List<Tag> Tags { get; set; }
}
.cshtml
文件中的单选按钮组如下所示:
<div class="form-group">
<label>Rating</label>
<p>The age rating</p>
@foreach (var rating in Model.Ratings)
{
<div class="form-check">
<input asp-for="Input.Rating" class="form-check-input" type="radio" id="@rating.Name.ToLower()" value="@rating.Id" checked>
<label class="form-check-label" for="@rating.Name.ToLower()">@rating.Name</label>
</div>
}
<span asp-validation-for="Input.Rating" class="text-warning"></span>
</div>
并且请求在发送表单时看起来也是 correct。
但是如果我把错误写出来
foreach (var msv in ModelState.Values)
{
foreach (var error in msv.Errors)
{
Console.WriteLine(error.ErrorMessage);
}
}
我收到错误
The Rating field is required.
写到控制台。
在 InputModel
中使用 int Rating
而不是完整的 Rating Rating
似乎已经解决了问题。
我在 ASP.NET Core 3.0 Razor 页面中使用单选按钮组时遇到一些麻烦。本质上,我想使用无线电组而不是 select,但我不知道如何将它正确绑定到我的 InputModel。
Code 用于有问题的 Razor 页面。当然还有 repo 中的其余代码。
在我的 cshtml.cs
中,我有一个像这样定义的输入模型
public class InputModel
{
[Required]
[StringLength(
CTConfig.Story.MaxTitleLength,
ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.",
MinimumLength = CTConfig.Story.MinTitleLength
)]
public string Title { get; set; }
[Required]
[StringLength(
CTConfig.Story.MaxDescriptionLength,
ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.",
MinimumLength = CTConfig.Story.MinDescriptionLength
)]
public string Description { get; set; }
[Required]
[StringLength(
CTConfig.Story.MaxHookLength,
ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.",
MinimumLength = CTConfig.Story.MinHookLength
)]
public string Hook { get; set; }
[DataType(DataType.Upload)]
[MaxFileSize(CTConfig.Story.CoverMaxWeight)]
[AllowedExtensions(new[] {".jpg", ".jpeg", ".png"})]
public IFormFile Cover { get; set; }
[Required]
public Rating Rating { get; set; }
[Required]
public List<Tag> Tags { get; set; }
}
.cshtml
文件中的单选按钮组如下所示:
<div class="form-group">
<label>Rating</label>
<p>The age rating</p>
@foreach (var rating in Model.Ratings)
{
<div class="form-check">
<input asp-for="Input.Rating" class="form-check-input" type="radio" id="@rating.Name.ToLower()" value="@rating.Id" checked>
<label class="form-check-label" for="@rating.Name.ToLower()">@rating.Name</label>
</div>
}
<span asp-validation-for="Input.Rating" class="text-warning"></span>
</div>
并且请求在发送表单时看起来也是 correct。
但是如果我把错误写出来
foreach (var msv in ModelState.Values)
{
foreach (var error in msv.Errors)
{
Console.WriteLine(error.ErrorMessage);
}
}
我收到错误
The Rating field is required.
写到控制台。
在 InputModel
中使用 int Rating
而不是完整的 Rating Rating
似乎已经解决了问题。