c# - 具有空值的必需字符串属性在 ASP.NET Core 2 Razor 页面中给出 IsValid=true

标签 c# asp.net-core asp.net-core-mvc model-validation

我真的很困惑。我在 ASP.NET Core 2 上有一个 Razor 页面,它有一个名为 SchemaId 的必需属性。我尝试将其标记为 [Required][BindRequired][Required(AllowEmptyStrings = false)],但当我发布时在我的表单中,我看到 SchemaIdnullModelState.IsValid == true。这是 Upload.cshtml.cs:

namespace Uploader.Pages
{
    public class UploadModel : PageModel
    {
        private IUploader _uploader;

        public UploadModel(IUploader uploader)
        {
            _uploader = uploader;
        }

        [BindProperty]
        public IEnumerable<IFormFile> UploadedFiles { get; set; }

        [Required(AllowEmptyStrings = false)]
        [BindProperty]
        [BindRequired]
        public string SchemaId { get; set; }


        public void OnGet(string schemaId = null)
        {
            SchemaId = schemaId;
        }

        public async Task<IActionResult> OnPostAsync()
        {
            // SchemaId is NULL right here!
            if (!ModelState.IsValid) // Yet IsValid = true!
            {
                return Page();
            }

            // Use _uploader to actually upload the file

            return RedirectToPage("/Next", new { uploadId = uploadId, schemaId = SchemaId });
        }
    }
}

Upload.cshtml 文件的相关摘录:

<div asp-validation-summary="All"></div>
<form enctype="multipart/form-data" method="POST" asp-page="Upload">
    <input class="inputfile" type="file" id="UploadedFiles" multiple="multiple" name="UploadedFiles">
    <label for="UploadedFiles">
        <span>Choose a file...</span>
    </label>
    <input type="hidden" asp-for="SchemaId">
    <button type="submit" class="inputfilesubmit">Upload</button>
</form>

如何让模型验证正常工作?

最佳答案

我想我找到了原因。

PageModel 中定义的

[BindRequired][Required] 属性似乎被忽略了。因此,您需要像下面这样验证模型:

public class Schema
{
    [Required]
    [BindRequired]
    public string Id { get; set; }
}
public class TestModel : PageModel
{
    [BindProperty]
    public Schema Schema { get; set; }

    public async Task<IActionResult> OnPostAsync()
    {
        // SchemaId is NULL right here!
        if (!ModelState.IsValid) // Yet IsValid = true!
        {
            return Page();
        }

        return Page();
    }

}

然后在cshtml中

<div asp-validation-summary="All"></div>
<form enctype="multipart/form-data" method="POST" asp-page="Upload">
    <input class="inputfile" type="file" id="UploadedFiles" multiple="multiple" name="UploadedFiles">
    <label for="UploadedFiles">
        <span>Choose a file...</span>
    </label>
    <input type="hidden" asp-for="Schema.Id">
    <button type="submit" class="inputfilesubmit">Upload</button>
</form>

我还没有找到任何提到这个的文档,但是在 Microsoft documentation 中他们还在单独的类中编写验证属性,所以我认为这种行为是意料之中的。

希望这能解决问题。

关于c# - 具有空值的必需字符串属性在 ASP.NET Core 2 Razor 页面中给出 IsValid=true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47118980/

相关文章:

c# - 列表框选择时自定义 WPF 绑定(bind)未更新

c# - 获取对象的AppDomain

C# 从子类中获取详细信息

javascript - Visual Studio 单页应用程序集成

c# - 将 Startup.cs 移动到类库(包)项目 - ASP.NET 5

c# - LINQ 与跳过和采取

css - Fontawesome 无法通过 css 中的引用编号加载图标

c# - DynamicRouteValueTransformer 不存在

jquery - 使用 IClientModelValidator 进行验证

asp.net-core - 如何使用 .NET Core 自定义 Web API 中的错误响应?