【发布时间】: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