我有一个 View ,其中包含自动生成的文本
类型的输入
框。当我单击“通过电子邮件发送结果”按钮时,代码会将您转到CalculatedResults Controller 中的EmailResults 操作。到目前为止,一切都很好。但是,即使我将 FormCollection 作为 EmailResults Action 中的参数,它仍然为空,我不知道为什么。我需要能够从表单中捕获文本框 - 请帮忙!在下面的 View 代码中,生成的文本框位于中间位置。
我的查看代码
@using (Html.BeginForm("EmailResults", "CalculatedResults", FormMethod.Post, new { data_ajax = "false" }))
{
<table>
<thead>
<tr>
<td>Ingredient</td>
<td>Qty (gm)</td>
</tr>
</thead>
@foreach (var i in Model)
{
if (Convert.ToDecimal(i.Qty) < 0)
{
<tr>
<td style="border: 1px solid red; color: red;">@i.Ingredient</td>
<td style="border: 1px solid red; color: red;">@i.Qty</td>
</tr>
}
else if (Convert.ToDecimal(i.Qty) == 0m)
{
continue;
}
else
{
if (i.Ingredient.Contains("Active"))
{
<tr>
<td>@i.Ingredient<br />
<input type="text" name="actives" /></td>
<td>@i.Qty</td>
</tr>
}
else
{
<tr>
<td>@i.Ingredient</td>
<td>@i.Qty</td>
</tr>
}
}
}
</table>
}
<div style="float: left">
@Html.ActionLink("Email Results", "EmailResults", "CalculatedResults", new { crpk = @ViewBag.crpk }, new { data_icon = "arrow-r", data_role = "button" })
</div>
我的 Controller 代码
public ViewResult EmailResults(int crpk, FormCollection collection)
{
CapWorxQuikCapContext context = new CapWorxQuikCapContext();
//List<string> variables = new List<string>();
//foreach (var item in Request.Form)
//{
// variables.Add(item.ToString());
//}
CalculatedResults cr = (from i in context.CalculatedResults where i.Pk == crpk select i).SingleOrDefault();
Helpers.Email.EmailResults(cr);
return View();
}
这是屏幕截图:
最佳答案
只有当您将表单提交到 Controller 时才会遇到 formCollection。要么
- 使用
<input type="submit" value="Email Results" />
- 将 jquery 处理程序连接到您创建的用于提交表单的 ActionLink。
关于c# - 尝试让我的文本字段在按下按钮时作为 FormCollection 传递,但 FormCollection 以 null 形式传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15962381/