简体   繁体   English

C# MVC 表单不将数据发送回 controller

[英]C# MVC form does not send data back to controller

I know it must be something stupid, but I cant seem to figure this out.我知道这一定是愚蠢的,但我似乎无法弄清楚。 I have the following in a view:我有以下观点:

 @using (@Html.BeginForm("ReceiveForm1", "Home", FormMethod.Post))
 {
     <div class="form-group">
         <label for="organizationID">Organization ID</label>
         <input type="number" class="form-control" asp-for="organizationID" aria-describedby="emailHelp" placeholder="Enter Organization ID">
     </div>
     <div class="form-group">
         <label for="externalPersonalID">External Personal ID</label>
         <input type="number" class="form-control" asp-for="externalPersonalID" placeholder="Enter External Personal ID">
     </div>
     <div class="form-group">
          <label for="phoneNumber">Phone Number</label>
          <input type="number" class="form-control" id="phoneNumber" asp-for="phoneNumber" placeholder="Password">
     </div>
     <button type="submit" class="btn btn-primary">Submit</button>
 }

And I have the following code in my controller:我的 controller 中有以下代码:

    [HttpPost]
    public ActionResult ReceiveForm1(FormCollection collection)
    {
        IEnumerable<Form1Return> personData = GetPersonInformation(collection);
        return View(personData);
    }

When I run the code, and enter data into the form, and click the submit button, I run to a breakpoint set on call to GetPersonInformation.当我运行代码并在表单中输入数据并单击提交按钮时,我运行到调用 GetPersonInformation 时设置的断点。 When I do a watch on collection, there are no elements.当我做手表收藏时,没有元素。 So for some reason, the form data is not making it to the controller.所以由于某种原因,表单数据没有进入 controller。 Any idea why?知道为什么吗?

Thanks.谢谢。

在此处输入图像描述

The 'Microsoft.AspNetCore.Mvc.ModelBinding.Binders.FormCollectionModelBinder' cannot bind to a model of type 'Microsoft.AspNetCore.Http.FormCollection'. “Microsoft.AspNetCore.Mvc.ModelBinding.Binders.FormCollectionModelBinder”无法绑定到“Microsoft.AspNetCore.Http.FormCollection”类型的 model。 Change the model type to 'Microsoft.AspNetCore.Http.IFormCollection' instead.将 model 类型改为“Microsoft.AspNetCore.Http.IFormCollection”。

Please try this:请试试这个:

    [HttpPost]
    public ActionResult ReceiveForm1(IFormCollection collection)
    {
        IEnumerable<Form1Return> personData = GetPersonInformation(collection);
        return View(personData);
    }

I've seen this happen when input 's don't have their name attribute set.input没有设置其name属性时,我已经看到了这种情况。 The relevant section on W3Schools has this nugget which confirms this is likely the case. W3Schools上的相关部分有这个金块,它证实了这很可能是这种情况。

Note: Only form elements with a name attribute will have their values passed when submitting a form.注意:只有具有 name 属性的表单元素才会在提交表单时传递其值。

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

相关问题 如何通过MVC C#中的控制器将模型数据发送到视图 - How to send model data to a view through a controller in MVC C# 从Jquery发送数据到控制器。 MVC C# - send Data to controller from Jquery. MVC C# 在 controller 中接收表单数据时出现问题。 C# - MVC - Problem receiving form data in the controller. C# - MVC C#MVC-控制器未从ViewModel表单接收所有数据 - C# MVC - Controller not receiving all data from ViewModel form c# asp.net core mvc 将数据从视图传递到控制器并从控制器返回到视图 - c# asp.net core mvc passing data from view to controller and from controller back to view MVC3剃刀局部视图不会发送回控制器 - MVC3 Razor Partial View does not send back to controller 无法通过javascript将数据发送回MVC控制器中的操作 - Unable to send back data to action in MVC controller from javascript 使用Ajax从Bootstrap模态将Razor Form数据作为模型传递回C#控制器方法 - Pass Razor Form data back to C# controller method as Model from bootstrap modal with ajax Send JSON data to highcharts pie from asp.net MVC controller in C# - Send JSON data to highcharts pie from asp.net MVC controller in C# 如何向 C# 中的 ASP.NET Core MVC controller 发送数据? - How to send data to the ASP.NET Core MVC controller in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM