我确定这是一个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();
}
答案 0 :(得分:0)
通过以下更改,我可以使该应用程序正常工作:
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}",
};
}