简体   繁体   English

ASP.NET Core 2.1 Razor 表单,发布未到达控制器

[英]ASP.NET Core 2.1 Razor Form, Post not reaching Controller

I spent a good margin of time searching and working through this problem yesterday without coming up with a solution.昨天我花了很多时间搜索和解决这个问题,但没有提出解决方案。 Here is the guide I used as reference. 这是我用作参考的指南。

The problem is that the data from the form is not reaching my controller.问题是表单中的数据没有到达我的控制器。 My goal is to take the form data and pass it to my controller/ model so that I can use the values throughout my code and store them in the database.我的目标是获取表单数据并将其传递给我的控制器/模型,以便我可以在整个代码中使用这些值并将它们存储在数据库中。 Below is what I have so far...以下是我到目前为止...

In my Browse.cshtml (View)在我的 Browse.cshtml(查看)中

@model Collect

<form asp-action="Collect" asp-controller="Collect" method="post">
<input type="hidden" asp-for="GameId" name="@game.id"/>
<button type="submit" class="dropdown-item btn btn-block">Default</button>
</form>

In my CollectController.cs (Controller)在我的 CollectController.cs(控制器)中

using System;
using GameLibrary.me.Models;
using Microsoft.AspNetCore.Mvc;

namespace GameLibrary.me.Controllers
{
public class CollectController  : Controller
{
    [HttpGet]
    public IActionResult Collect()
    {
        return View();
    }

    [HttpPost, ValidateAntiForgeryToken]
    public IActionResult Collect(Collect model)
    {

        Console.WriteLine("**********\n"+model.GameId+"\n**********");

        return Content($"Hello {model.GameId}");
    }    
  }
}

In my Collect.cs (Model)在我的 Collect.cs(模型)中

namespace GameLibrary.me.Models

{
public class Collect
{
    public int GameId { get; set; }
  }
}

EDIT: Here is what my IDE is telling me...编辑:这是我的 IDE 告诉我的......

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
  Request starting HTTP/1.1 POST https://localhost:5001/browse?game=eevee application/x-www-form-urlencoded 7

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
  Request finished in 1.0139ms 200

info: Microsoft.AspNetCore.Server.Kestrel[32]
  Connection id "0HLJHIUOU6AKO", Request id "0HLJHIUOU6AKO:00000003": the application completed without reading the entire request body.

Any guidance on what I am doing wrong would be great appreciated... Also can I send multiple values through the hidden field type, or should I make a new hidden field type for each value?任何关于我做错了什么的指导将不胜感激......我还可以通过隐藏字段类型发送多个值,还是应该为每个值创建一个新的隐藏字段类型?

There was a lot of different help here, thanks especially to Kirk Larklin!这里有很多不同的帮助,特别感谢 Kirk Larklin! There were three issues that was preventing my controller from picking up the data.有三个问题阻止了我的控制器获取数据。

  1. Browse.cshtml was missing the @addTagHelpers... I added the following: Browse.cshtml 缺少 @addTagHelpers ... 我添加了以下内容:

     @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers @addTagHelper *, AuthoringTagHelpers
  2. My CollectController.cs was missing a route... I added the following:我的 CollectController.cs 缺少路线...我添加了以下内容:

     [HttpPost, ValidateAntiForgeryToken] [Route("Index/Collect")]
  3. Finally, I renamed my controller post method from 'Collect' which conflicting with another method to Index and updated the asp-action in my Browse.CSHTML file to match.最后,我将我的控制器 post 方法从与另一个方法冲突的“Collect”重命名为 Index,并更新了 Browse.CSHTML 文件中的 asp-action 以匹配。

     public IActionResult Index(Collect model)

Thanks for all the help!感谢所有的帮助!

-Travis W -特拉维斯W

First I would pass in the model to the view with the Id initialised:首先,我会将模型传递给初始化了 Id 的视图:

    public IActionResult Collect()
    {

        return View(new Collect { GameId = "5"});
    }

Inside your view update the form to the following:在您的视图中,将表单更新为以下内容:

<form asp-action="Collect" asp-controller="Collect" method="post">
    @Html.HiddenFor(m => m.GameId)
    <button type="submit" class="dropdown-item btn btn-block">Default</button>
</form>

The Html helper will create the html coded for the field. Html 助手将创建为该字段编码的 html。 Once you click submit the value will be correct.单击提交后,该值将是正确的。

Based on your example I do not believe that you want custom actions, you should be able to achieve your goal with the default framework mapping.根据您的示例,我不相信您想要自定义操作,您应该能够使用默认框架映射来实现您的目标。

public class CollectController : Controller
{
     [HttpGet]
     public IActionResult Index() => View();

     [HttpPost]
     public IActionResult Index([FromBody]CollectionModel model) => Content(...);
}

To clarify the magic, the declared Post method I utilize a HttpBody attribute, otherwise the framework will expect the parameter contents to be defined via Query String in the URL.为了阐明这个魔法,我使用了一个HttpBody属性声明的 Post 方法,否则框架将期望通过 URL 中的查询字符串定义参数内容。 The framework by default looks for Index when a Controller has been hit, if you do not need the URL to represent that, then do not use it.默认情况下,当控制器被命中时,框架会查找Index ,如果您不需要 URL 来表示,则不要使用它。

The routing pattern usually follows:路由模式通常如下:

routes.MapRoute(
                "Default",                         // Route name
                "{controller}/{action}/{id}",      // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

URL:网址:

http://www.sample.com/Collect ---> Initial index Get would be hit http://www.sample.com/Collect ---> 初始索引 Get 将被命中

In my case, I updated the login view with this line of code.就我而言,我用这行代码更新了登录视图。 that's fixed.那是固定的。

<form method="post" asp-controller="account" asp-action="login">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM