C# ASP.NET-MVC 中 Form Post 上的 FormCollection 为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/504681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FormCollection Empty on Form Post in ASP.NET-MVC
提问by Mark Struzinski
I am posting a very simple form using a method I have used frequently in the past. It may be easier to show my code rather than type a lengthy explanation. Here's the HTML:
我使用过去经常使用的方法发布了一个非常简单的表单。显示我的代码可能比输入冗长的解释更容易。这是 HTML:
<% Html.BeginForm("CreateMarketingType", "ListMaintenance"); %>
<div id="ListMaintenanceContainer">
<table>
<tr>
<th>Marketing Type Id</th>
<th>Marketing Type Name</th>
</tr>
<%foreach (MarketingType marketingType in ViewData.Model.MarketingTypes) %>
<%{ %>
<tr>
<td><%= marketingType.MarketingTypeId.ToString() %></td>
<td><%= marketingType.MarketingTypeName %></td>
</tr>
<%} %>
</table>
<div>
<fieldset id="fsSaveNewMarketingType">
<legend>Add New Marketing Type</legend>
<label for="txtNewMarketingTypeName">New Marketing Type Name:</label>
<input type="text" id="txtNewMarketingTypeName" />
<input type="submit" value="Save" id="CreateMarketingType" />
</fieldset>
</div>
</div>
<% Html.EndForm();%>
And here's the controller code:
这是控制器代码:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateMarketingType(FormCollection form)
{
string newMarketingTypeName = Request.Form["txtNewMarketingTypeName"].ToString();
MarketingType newMarketingType = new MarketingType() { MarketingTypeName = newMarketingTypeName };
_marketingTypeRepository.AddNewMarketingType(newMarketingType);
return View("ListMaintenance", GetModel());
}
The submit button posts the form, and the method is invoked, but the form object defined in my parameter is empty. I have also tried Request.Form and I get the same result. Am I missing something here?
提交按钮发布表单,并调用该方法,但我的参数中定义的表单对象为空。我也试过 Request.Form 并得到相同的结果。我在这里错过了什么吗?
采纳答案by Craig Stuntz
None of your inputs have a name attribute. No name = not in the FormCollection.
您的所有输入都没有 name 属性。无名称 = 不在 FormCollection 中。
回答by dudeNumber4
Wish I could post this as a simple comment, but I don't have that priviledge... I added all my name attributes, and still no joy. Remember to add your name attribute to your form itself. Must use the overload for HTML.BeginForm that accepts htmlAttributes.
希望我能把它作为一个简单的评论发布,但我没有那个特权......我添加了我所有的名字属性,但仍然没有快乐。请记住将您的 name 属性添加到表单本身。必须对接受 htmlAttributes 的 HTML.BeginForm 使用重载。
回答by ben
I had this issue and then realised I had disabled all the INPUT controls before I submitted the form (as a UX feature).
我遇到了这个问题,然后意识到我在提交表单之前禁用了所有 INPUT 控件(作为 UX 功能)。
回答by LCJ
I see that this question is already answered. Following is another approach I used in a MVC view, when the form collection was empty. Using JavaScript / jQuery, it sets name-value pairs to a hidden control which gets appended to the form.
我看到这个问题已经回答了。以下是我在 MVC 视图中使用的另一种方法,当表单集合为空时。使用 JavaScript / jQuery,它将名称-值对设置为附加到表单的隐藏控件。
JavaScript / jQuery
JavaScript / jQuery
$("#btnExport").click(function (event) {
event.preventDefault();
//Append the parameter to the form
var practiceVal = $('#Practice').val();
$('<input />').attr('type', 'hidden')
.attr('name', "Practice").attr('value', practiceVal)
.appendTo('#formHome');
//Submit
$('#formHome').submit();
});
Form
形式
<form id="formHome" method="post">
</form>
Refer Forms
参考表格
When a form is submitted for processing, some controls have their
name
paired with their current value and these pairs are submitted with the form
当提交表单进行处理时,一些控件将其
name
与当前值配对,这些对与表单一起提交