FormCollection Empty on Form Post in ASP.NET-MVC


Question

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?

1
30
2/2/2009 8:12:24 PM

Accepted Answer

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

89
2/2/2009 8:24:45 PM

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


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon