英文:
Keep the form data when validation fails
问题
以下是您要翻译的内容:
我正在尝试验证一个ASP.NET Razor页面项目中的输入。
当我的模型验证失败时,我如何保留表单数据?
当我的模型验证失败时,选择字段和可供选择的项目列表都为空。
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Loan.Book" class="control-label"></label>
<select asp-for="Loan.BookID" class="form-control" asp-items="ViewBag.Books"></select>
</div>
<div class="form-group">
<label asp-for="Loan.Reader" class="control-label"></label>
<select asp-for="Loan.ReaderID" class="form-control" asp-items="ViewBag.Readers"></select>
</div>
<div class="form-group">
<label asp-for="Loan.DueDate" class="control-label"></label>
<input asp-for="Loan.DueDate" class="form-control" />
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
C# 代码
public IActionResult OnGet()
{
ViewData["Books"] = new SelectList(_context.Book, "ID", "Title");
ViewData["Readers"] = new SelectList(_context.Reader, "ID", "Name");
return Page();
}
[BindProperty]
public Loan Loan { get; set; } = default!;
// 为了防止过多的攻击,请参阅 https://aka.ms/RazorPagesCRUD
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid || _context.Loan == null || Loan == null)
{
return Page();
}
Book book = await _context.Book.FindAsync(Loan.BookID);
if (book.InStock <= 0)
{
ModelState.AddModelError("Loan.Book", "This book is out of stock");
return Page();
}
book.InStock--;
_context.Loan.Add(Loan);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
空选择框截图:
英文:
I am trying to validate input in an ASP.NET razor pages project.
How can I keep the form data when my model validation fails?
When my model validation fails, both the select fields and the list of items to choose from are empty.
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Loan.Book" class="control-label"></label>
<select asp-for="Loan.BookID" class ="form-control" asp-items="ViewBag.Books"></select>
</div>
<div class="form-group">
<label asp-for="Loan.Reader" class="control-label"></label>
<select asp-for="Loan.ReaderID" class="form-control" asp-items="ViewBag.Readers"></select>
</div>
<div class="form-group">
<label asp-for="Loan.DueDate" class="control-label"></label>
<input asp-for="Loan.DueDate" class="form-control" />
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
C# Code
public IActionResult OnGet()
{
ViewData["Books"] = new SelectList(_context.Book, "ID", "Title");
ViewData["Readers"] = new SelectList(_context.Reader, "ID", "Name");
return Page();
}
[BindProperty]
public Loan Loan { get; set; } = default!;
// To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid || _context.Loan == null || Loan == null)
{
return Page();
}
Book book = await _context.Book.FindAsync(Loan.BookID);
if (book.InStock <= 0)
{
ModelState.AddModelError("Loan.Book", "This book is out of stock");
return Page();
}
book.InStock--;
_context.Loan.Add(Loan);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
Empty select secreenshot:
答案1
得分: 0
以下是翻译好的部分:
要保持可选择的项目列表不为空,您需要在您的 OnPostAsync()
方法中添加以下代码:
ViewData["Books"] = new SelectList(_context.Book, "ID", "Title", Loan.BookID);
ViewData["Readers"] = new SelectList(_context.Reader, "ID", "Name", Loan.ReaderID);
英文:
To keep the list of items to choose from are not empty,
you need to add below code in your OnPostAsync()
:
ViewData["Books"] = new SelectList(_context.Book, "ID", "Title",Loan.BookID);
ViewData["Readers"] = new SelectList(_context.Reader, "ID", "Name",Loan.ReaderID);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论