保留表单数据当验证失败。

huangapple go评论65阅读模式
英文:

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.

&lt;div class=&quot;row&quot;&gt;
    &lt;div class=&quot;col-md-4&quot;&gt;
        &lt;form method=&quot;post&quot;&gt;
            &lt;div asp-validation-summary=&quot;ModelOnly&quot; class=&quot;text-danger&quot;&gt;&lt;/div&gt;
            &lt;div class=&quot;form-group&quot;&gt;
                &lt;label asp-for=&quot;Loan.Book&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                &lt;select asp-for=&quot;Loan.BookID&quot; class =&quot;form-control&quot; asp-items=&quot;ViewBag.Books&quot;&gt;&lt;/select&gt;
            &lt;/div&gt;
            &lt;div class=&quot;form-group&quot;&gt;
                &lt;label asp-for=&quot;Loan.Reader&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                &lt;select asp-for=&quot;Loan.ReaderID&quot; class=&quot;form-control&quot; asp-items=&quot;ViewBag.Readers&quot;&gt;&lt;/select&gt;
            &lt;/div&gt;
            &lt;div class=&quot;form-group&quot;&gt;
                &lt;label asp-for=&quot;Loan.DueDate&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                &lt;input asp-for=&quot;Loan.DueDate&quot; class=&quot;form-control&quot; /&gt;
            &lt;/div&gt;
            &lt;div class=&quot;form-group&quot;&gt;
                &lt;input type=&quot;submit&quot; value=&quot;Create&quot; class=&quot;btn btn-primary&quot; /&gt;
            &lt;/div&gt;
        &lt;/form&gt;
    &lt;/div&gt;
&lt;/div&gt;

C# Code

public IActionResult OnGet()
{
    ViewData[&quot;Books&quot;] = new SelectList(_context.Book, &quot;ID&quot;, &quot;Title&quot;);
    ViewData[&quot;Readers&quot;] = new SelectList(_context.Reader, &quot;ID&quot;, &quot;Name&quot;);
    return Page();
}

[BindProperty]
public Loan Loan { get; set; } = default!;


// To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
public async Task&lt;IActionResult&gt; OnPostAsync()
{
    if (!ModelState.IsValid || _context.Loan == null || Loan == null)
    {
        return Page();
    }

    Book book = await _context.Book.FindAsync(Loan.BookID);

    if (book.InStock &lt;= 0)
    {
        ModelState.AddModelError(&quot;Loan.Book&quot;, &quot;This book is out of stock&quot;);
        return Page();
    }

    book.InStock--;

    _context.Loan.Add(Loan);
    await _context.SaveChangesAsync();

    return RedirectToPage(&quot;./Index&quot;);
}

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[&quot;Books&quot;] = new SelectList(_context.Book, &quot;ID&quot;, &quot;Title&quot;,Loan.BookID);
ViewData[&quot;Readers&quot;] = new SelectList(_context.Reader, &quot;ID&quot;, &quot;Name&quot;,Loan.ReaderID);

huangapple
  • 本文由 发表于 2023年4月11日 05:00:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定