TextArea 在 Razor 页面上失去价值 post
TextArea loses value on Razor Page post
我确定这是一个 ASP.Net 核心新手问题,但我已经在这上面花了几个小时,我不想再花时间在这上面了。
我有一个表单中的文本区域,在第一次获取时运行良好,但是一旦我 post 表单,文本区域就会丢失其文本。
我错过了什么?
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
我更改了代码以在页面的模型绑定中使用 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();
}
}
通过进行以下更改,我能够使应用程序正常工作:
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}",
};
}
我确定这是一个 ASP.Net 核心新手问题,但我已经在这上面花了几个小时,我不想再花时间在这上面了。
我有一个表单中的文本区域,在第一次获取时运行良好,但是一旦我 post 表单,文本区域就会丢失其文本。 我错过了什么?
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 我更改了代码以在页面的模型绑定中使用 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();
}
}
通过进行以下更改,我能够使应用程序正常工作:
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}",
};
}