Trying to get my text fields to pass through as a FormCollection upon button press, but FormCollection is coming through as null

StackOverflow https://stackoverflow.com/questions/15962381

Question

I have a View that contains automatically generated input boxes of type text. When I click the "Email Results" button, the code takes you over to the EmailResults Action within the CalculatedResults controller. So far, so good. But, even though I have FormCollection as a parameter in the EmailResults Action, it's coming through as null, and I don't know why. I need to be able to capture the text boxes from the form- please help!!. In my View code below, the generated text boxes are a little past halfway down.

My View Code

@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>

My Controller Code

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();
}

Here's a screenshot:

enter image description here

Was it helpful?

Solution

The formCollection will only come across when you submit the form to the controller. Either

  1. Use <input type="submit" value="Email Results" />
  2. Wire up a jquery handler to the ActionLink that you have created that submits the form for you.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top