【问题标题】:TextArea loses value on Razor Page postTextArea 在 Razor Page 帖子上失去价值
【发布时间】:2019-09-24 14:49:58
【问题描述】:

我确信这是一个 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();
    }

更新:2019 年 5 月 6 日 我将代码更改为在页面的模型绑定中使用 List,但现在我无法将 list 属性绑定到控件。

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

}

【问题讨论】:

  • 浏览器不会将disabled 元素的值发送到服务器。相反,您可以使用readonly
  • 感谢@cem 您的意见。我学到了一些新东西。

标签: c# asp.net-core asp.net-core-2.0 razor-pages asp.net-core-2.2


【解决方案1】:

通过进行以下更改,我能够使应用程序正常工作:

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}",
      };
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 2011-08-19
    • 2021-02-21
    • 2013-07-30
    • 1970-01-01
    • 2010-09-17
    相关资源
    最近更新 更多