c# - 尝试让我的文本字段在按下按钮时作为 FormCollection 传递,但 FormCollection 以 null 形式传递

标签 c# asp.net asp.net-mvc asp.net-mvc-4 formcollection

我有一个 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();
}

这是屏幕截图:

enter image description here

最佳答案

只有当您将表单提交到 Controller 时才会遇到 formCollection。要么

  1. 使用<input type="submit" value="Email Results" />
  2. 将 jquery 处理程序连接到您创建的用于提交表单的 ActionLink。

关于c# - 尝试让我的文本字段在按下按钮时作为 FormCollection 传递,但 FormCollection 以 null 形式传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15962381/

相关文章:

css - 如何使用 WebOptimization 和 Bundling 将 CSS 添加到 ASP.NET 站点

asp.net - 在 FileUpload 控件中获取文件的扩展名

javascript - Jquery 在 cshtml 页面中不工作

css - ASP.NET Core MVC - 未应用内联 css 样式

c# - CaptureSource.Start() 在 Windows Phone Silverlight 8.1 中抛出 System.UnauthorizedAccessException

c# - 我可以让 XmlSerializer 在反序列化时忽略命名空间吗?

c# - 检索一个类的所有 bool 属性的名称,这些属性为真

c# - 从 C# 运行 Powershell

asp.net - Asp.net 的模态弹出图像查看器

c# - 使用也接受空的属性验证密码长度?