[英]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.