TextArea теряет значение в сообщении Razor Page - PullRequest
0 голосов
/ 07 мая 2019

Я уверен, что это вопрос новичка ASP.Net Core, но я потратил пару часов на это, и я не хочу больше тратить время на это.

У меня есть текстовая область в форме, которая хорошо работает при первом получении, но как только я отправляю форму, текстовая область теряет свой текст.Что мне не хватает?

Settings.cshtml:

<form role="form" id="account" method="post">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="form-control-label">Existing keywords</label>
<textarea rows="@Model.PredefinedKeywordsCount" class="form-control form-control-muted" disabled="disabled">@Model.PredefinedKeywordsSeperatedByNewLine</textarea>
</div>
</div>
</div>
</form>

Settings.cshtml.cs:

public class SettingsModel : PageModel
  {
public class InputModel
    {

    }
[BindProperty]
    public string PredefinedKeywordsSeperatedByNewLine { get; set; }
    [BindProperty]
    public int PredefinedKeywordsCount { get; set; }
    [BindProperty]
    public InputModel Input { get; set; }
public async Task OnGetAsync()
    {
      var predefinedKeywords = <GetListFromDatabaseOperation>;
      PredefinedKeywordsSeperatedByNewLine = predefinedKeywords.GetListSeperatedByNewLineAsync();
      PredefinedKeywordsCount = predefinedKeywords.Count;
    }    
public async Task<IActionResult> OnPostAsync()
    {
      if (ModelState.IsValid)
      {
//Some code to save new keywords in database
      }
      return Page();
    }

Обновление: 5/6/2019 Я изменил код для использования списка в привязке модели страницы, но теперь не могу привязать свойство списка к элементу управления.

Settings.cshtml:

<form role="form" id="account" method="post">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="form-control-label">Existing keywords</label>
<textarea asp-for="Input.PredefinedKeywords" class="form-control form-control-muted" rows="@Model.Input.PredefinedKeywords.GetListSeperatedByNewLineAsync()"                                                  disabled="disabled">@Model.Input.PredefinedKeywords.Count</textarea>
</div>
</div>
</div>
</form>

Settings.cshtml.cs:

 public class SettingsModel : PageModel
  {
public class InputModel
    {
      public string NewKeywords { get; set; }
      public List<PredefinedKeyword> PredefinedKeywords { get; set; }
    }
[BindProperty]
    public InputModel Input { get; set; }
    public async Task OnGetAsync()
    {
      this.Input = new InputModel
      {
        PredefinedKeywords = await ScrubberDbContext.PredefinedKeywords.ToListAsync()
      };
    }
    public async Task<IActionResult> OnPostAsync()
    {
          if (ModelState.IsValid)
          {
    //Some code to save new keywords in database
          }
          return Page();
        }

} enter image description here

1 Ответ

0 голосов
/ 07 мая 2019

Мне удалось заставить приложение работать, внеся следующие изменения:

Settings.cshtml:

<form role="form" id="account" method="post">
    <div class="row">
    <div class="col-md-6">
    <div class="form-group">
    <label class="form-control-label">Existing keywords</label>
    @{var predefinedKeywordsArray=Model.Input.PredefinedKeywords.Split("|");
string predefinedKeywords = predefinedKeywordsArray[0];
int rows = Int32.Parse(predefinedKeywordsArray[1]);}
<textarea asp-for="@predefinedKeywords" class="form-control form-control-muted" rows="@rows" readonly></textarea>
    </div>
    </div>
    </div>
    </form>

Settings.cshtml.cs:

public class SettingsModel : PageModel
  {
public class InputModel
    {
      public string NewKeywords { get; set; }
      public string PredefinedKeywords { get; set; }
    }
[BindProperty]
    public InputModel Input { get; set; }
    public async Task OnGetAsync()
    {
      var predefinedKeywords = <GetListFromDatabaseOperation>;
      this.Input = new InputModel
      {
        PredefinedKeywords = $"{predefinedKeywords.GetListSeperatedByNewLineAsync()}|{predefinedKeywords.Count}",
      };
    }
...