ASP.NET MVC 项目:为什么我的表单无法访问我的控制器方法?

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

ASP.NET MVC Project: Why is my form not accessing my controller method?

问题

当我尝试提交我的'Create.cshtml'文件中的表单时,我被引导到一个"HTTP ERROR 405"页面。提交根本没有访问控制器中的函数。

这是控制器的POST函数:

// POST: Books/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(CreateViewModel model)
{
    Console.WriteLine("Hello from create post function");
    // 空值检查
    if (string.IsNullOrEmpty(model.title) || string.IsNullOrEmpty(model.author) || model.yearPublished == null)
        return NotFound();

    // 创建图书
    var book = new Book {
        Id = Guid.NewGuid(),
        Title = model.title,
        Author = model.author,
        YearPublished = model.yearPublished
    };

    // 检查图书ID是否唯一
    while (true) {
        
        // 查看图书是否存在
        var temp_book = await _context.Books.FirstOrDefaultAsync(b => b.Id == book.Id);

        // 如果不存在,中断循环,否则重新分配GUID
        if (temp_book == null)
            break;
        else
            book.Id = Guid.NewGuid();
    }

    // 将图书添加到Books表格并保存
    _context.Books.Add(book);
    await _context.SaveChangesAsync();
    return RedirectToAction(nameof(Index));
}

这是带有无法连接的表单的'Create.cshtml'文件:

@model Deliverable4.Controllers.CreateViewModel

@{
    ViewData["Title"] = "Create";
}

<h1>@ViewData["Title"]</h1>

<div>
    <p>CREATE PAGE</p>
</div>

<form asp-action="Create" method="post">
    <label for="title">Title</label>
    <input name="title" id="title">

    <label for="author">Author</label>
    <input name="author" id="author">

    <label for="yearPublished">Year</label>
    <input name="yearPublished" id="yearPublished">

    <input type="submit" value="Submit">
</form>

有人能告诉我问题是什么吗?

英文:

When I try and submit the form in my 'Create.cshtml' file, I am brough to a "HTTP ERROR 405" page. The submission is not at all accessing the function in the controller.

Here is the controller post function:

        // POST: Books/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task&lt;IActionResult&gt; Create(CreateViewModel model)
        {
            Console.WriteLine(&quot;Hello from create post function&quot;);
            // null check
            if (string.IsNullOrEmpty(model.title) || string.IsNullOrEmpty(model.author) || model.yearPublished == null)
                return NotFound();

            // Create Book
            var book = new Book {
                Id = Guid.NewGuid(),
                Title = model.title,
                Author = model.author,
                YearPublished = model.yearPublished
            };

            // Check if book ID is original
            while (true) {
                
                // see if book exists
                var temp_book = await _context.Books.FirstOrDefaultAsync(b =&gt; b.Id == book.Id);

                // if it does break, else reassign the GUID
                if (temp_book == null)
                    break;
                else
                    book.Id = Guid.NewGuid();
            }

            // Save book to Books Table + Save
            _context.Books.Add(book);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

And here is the 'Create.cshtml' file with the form that is not connecting:

@model Deliverable4.Controllers.CreateViewModel

@{
    ViewData[&quot;Title&quot;] = &quot;Create&quot;;
}

&lt;h1&gt;@ViewData[&quot;Title&quot;]&lt;/h1&gt;

&lt;div&gt;
    &lt;p&gt;CREATE PAGE&lt;/p&gt;
&lt;/div&gt;

&lt;form asp-action=&quot;Create&quot; method=&quot;post&quot;&gt;
    &lt;label for=&quot;title&quot;&gt;Title&lt;/label&gt;
    &lt;input name=&quot;title&quot; id=&quot;title&quot;&gt;

    &lt;label for=&quot;author&quot;&gt;Author&lt;/label&gt;
    &lt;input name=&quot;author&quot; id=&quot;author&quot;&gt;

    &lt;label for=&quot;yearPublished&quot;&gt;Year&lt;/label&gt;
    &lt;input name=&quot;yearPublished&quot; id=&quot;yearPublished&quot;&gt;

    &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;

Can anyone tell me the issue?

答案1

得分: 1

问题似乎是你告诉控制器验证AntiForgeryToken,但在表单中没有提供AntiForgeryToken。在你的表单中添加AntiForgeryToken,错误应该会解决。

&lt;form method=&quot;post&quot; action=&quot;Create&quot; &gt;
@Html.AntiForgeryToken()
&lt;!--你的表单其余部分--&gt;
&lt;/form&gt;
英文:

The problem seems to be that you tell your controller to validate the AntiForgeryToken but you do not provide the AntiForgeryToken in your Form. Add the AntiForgeryToken in your Form and the error should be resolved.

&lt;form method=&quot;post&quot; action=&quot;Create&quot; &gt;
@Html.AntiForgeryToken()
&lt;!--rest of your Form elements--&gt;
&lt;/form&gt;

</details>



huangapple
  • 本文由 发表于 2023年7月7日 03:36:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632059.html
匿名

发表评论

匿名网友

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

确定