32

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.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?

Mark Struzinski
  • 32,945
  • 35
  • 107
  • 137

4 Answers4

93

None of your inputs have a name attribute. No name = not in the FormCollection.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • 9
    Ouch - that's what I get for growing up on WebForms!! I thought the id attribute passed the values through on the form. Thanks for the reply!! – Mark Struzinski Feb 02 '09 at 20:33
  • This is surprising as everything has seemingly been pointing towards using the id only. Things not as they seemed..to me anyway.. – Mark Redman Apr 06 '10 at 09:19
  • 12
    @Mark, this is required per the HTTP spec. `id` = how to find it in the DOM. `name` = what gets submitted in the `form`. – Craig Stuntz Apr 06 '10 at 12:42
  • 3
    Sure enough, he's right! (Link love: http://www.w3.org/TR/html401/interact/forms.html#h-17.2 ) controls need a 'name' attribute when posting. +1 for helping me stop pull my hair out. – Dan Esparza Aug 13 '10 at 03:53
  • 1
    This really help me a lot. In my case, I have tried several methods to submit the form, but the formcollection content was always empty. After trying the add the name attribute in the tag, it works. Great! – lzlstyle Dec 21 '13 at 09:58
3

I had this issue and then realised I had disabled all the INPUT controls before I submitted the form (as a UX feature).

ben
  • 1,448
  • 1
  • 17
  • 12
  • When you disable the fields, does that remove them from the FormCollection? I seem to be having the same problem. – WEFX Dec 19 '11 at 17:50
0

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.

dudeNumber4
  • 4,217
  • 7
  • 40
  • 57
0

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.

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

LCJ
  • 22,196
  • 67
  • 260
  • 418