.NET Core 2.1 Razor页面ModelState IsValid在发布时为false

时间:2018-11-25 20:47:41

标签: razor modelstate asp.net-core-mvc-2.1

我遇到了无法找到解决方案的ModelState验证问题。我的页面加载如下:

[BindProperty]
public ApplicationLanguage UpdatedLanguage { get; set; }
[BindProperty]
public IList<ApplicationTranslation> ApplicationTranslations { get; set; }
public async Task<IActionResult> OnGetAsync(Guid id)
{
    if (ModelState.IsValid)
    {
        {
            UpdatedLanguage = await _languageService.FindByIdAsync(HttpContext, id);
            ApplicationTranslations = await _translationService.GetAllTranslationsAsync(UpdatedLanguage);
        }
    }
    return Page();
}

这将正确填充以下页面:

<h4>@Model.UpdatedLanguage.Name</h4>
<hr />
<div class="container">
    <div class="form-group">
        <input asp-for="UpdatedLanguage.Id" hidden="hidden" />
    </div>
    <form method="post">
        <div class="row">
            @for (int i = 0; i < Model.ApplicationTranslations.Count; i = i + 2)
            {
                <div class="col-sm-3">
                    <label> @Model.ApplicationTranslations[i].Tag</label>
                </div>
                <div class="col-sm-3">
                    <div class="form-group">
                        <input asp-for="ApplicationTranslations[i].Translation" class="form-control" />
                        <span asp-validation-for="ApplicationTranslations[i].Translation" class="text-danger"></span>
                    </div>
                </div>

                <div class="col-sm-3">
                    @if (i + 1 < Model.ApplicationTranslations.Count)
                    {
                        <label> @Model.ApplicationTranslations[i + 1].Tag</label>
                    }
                </div>
                <div class="col-sm-3">
                    @if (i + 1 < Model.ApplicationTranslations.Count)
                    {
                        <div class="form-group">
                            <input asp-for="ApplicationTranslations[i+1].Translation" class="form-control" />
                            <span asp-validation-for="ApplicationTranslations[i+1].Translation" class="text-danger"></span>
                        </div>
                    }
                </div>
            }
        </div>
        <div class="row">
            <button type="submit" class="btn btn-default">@WebTranslator.Translate(HttpContext, "Save")</button>
        </div>
    </form>
</div>

但是当我保存表单时,我有一个无效的ModelState:

public async Task<IActionResult> OnPostAsync(ApplicationLanguage UpdatedLanguage, IList<ApplicationTranslation> ApplicationTranslations)
{
    if (!ModelState.IsValid)
    {
        return Page();
    }
 List<ApplicationTranslation> OriginalApplicationTranslations = (List<ApplicationTranslation>)(await _translationService.GetAllTranslationsAsync(UpdatedLanguage));
    foreach (ApplicationTranslation translation in ApplicationTranslations)
    {
        ApplicationTranslation originalTranslation = OriginalApplicationTranslations.Find(t => t.Tag == translation.Tag);
        IResult result = new ApplicationResult();
        if (result.Succeeded && originalTranslation.Translation != translation.Translation)
        {
            result = (await _translationService.UpdateAsync(HttpContext, translation));
        }
    }
return RedirectToPage("./Languages");
}

当进行调试时,我发现在ModelState值上,ApplicationLanguage.Id值有效,但是ApplicationLanguage.Name和所有ApplicationTranslations无效,但我不知道为什么。

0 个答案:

没有答案