.NET Core 2.1 Razor Page ModelState IsValid false on Post












0















I'm with a ModelState validation issue that I can't find the solution. I have the following page load:



[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();
}


This populates correctly the following 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>


But when I save the form I have an invalid 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");
}


When debbugin, I found that on ModelState Values, the ApplicationLanguage.Id value is valid, but the ApplicationLanguage.Name and all ApplicationTranslations are invalid but I don't know why.










share|improve this question





























    0















    I'm with a ModelState validation issue that I can't find the solution. I have the following page load:



    [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();
    }


    This populates correctly the following 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>


    But when I save the form I have an invalid 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");
    }


    When debbugin, I found that on ModelState Values, the ApplicationLanguage.Id value is valid, but the ApplicationLanguage.Name and all ApplicationTranslations are invalid but I don't know why.










    share|improve this question



























      0












      0








      0


      1






      I'm with a ModelState validation issue that I can't find the solution. I have the following page load:



      [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();
      }


      This populates correctly the following 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>


      But when I save the form I have an invalid 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");
      }


      When debbugin, I found that on ModelState Values, the ApplicationLanguage.Id value is valid, but the ApplicationLanguage.Name and all ApplicationTranslations are invalid but I don't know why.










      share|improve this question
















      I'm with a ModelState validation issue that I can't find the solution. I have the following page load:



      [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();
      }


      This populates correctly the following 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>


      But when I save the form I have an invalid 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");
      }


      When debbugin, I found that on ModelState Values, the ApplicationLanguage.Id value is valid, but the ApplicationLanguage.Name and all ApplicationTranslations are invalid but I don't know why.







      razor modelstate asp.net-core-mvc-2.1






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 20:54







      Matias

















      asked Nov 25 '18 at 20:47









      MatiasMatias

      208




      208
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471807%2fnet-core-2-1-razor-page-modelstate-isvalid-false-on-post%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471807%2fnet-core-2-1-razor-page-modelstate-isvalid-false-on-post%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin