views:

492

answers:

3

I have a simple form on a view page, implemented as a user control, that looks something like this:

<%=Html.BeginForm("List", "Building", FormMethod.Post) %>

//several fields go here

<%Html.EndForm(); %>

There are two problems I would like resolved, the first is that I would like the controller method that receives this to take a type parameter of the user control. The goal is to avoid putting all of the fields of the form into the parameter list for the method. The controller method currently looks like this:

[AcceptVerbs("Post")]
    public ActionResult List(string capacityAmount)
    {
        ProfilerDataDataContext context = new ProfilerDataDataContext();
        IEnumerable<Building> result = context.Buildings.OrderBy(p => p.SchoolName);
        ViewData["Boroughs"] = new SelectList(Boroughs.BoroughsDropDown());

        return View(result);
    }

The rest of the fields in the form will be used to conduct a search against the buildings type.

The form posts fine, I can search on the capacity the way you would expect, but I can smell ugliness ahead as I add parameters to the search.

Second, smaller problem is that when the page renders the BeginForm tag renders the string "System.Web.Mvc.Form" to the page. How do I make that go away?

+6  A: 

1) Use FormCollection as the argument:

public ActionResult List(FormCollection searchQuery)

Now you can iterate the FormCollection and get key/value search terms from your search form.

2) Remove the "=" from BeginForm:

<% Html.BeginForm("List", "Building", FormMethod.Post) %>

That said, you should really be using, um... using:

<% using (Html.BeginForm("List", "Building", FormMethod.Post)) { %>
<% } %>
Craig Stuntz
I am using your using suggestion, but the controller method never fires. I must be missing something else...
KevDog
What *does* happen when you submit?
Craig Stuntz
If nothing happens, then I would take a very careful look at what is submitted to the server. I generally use firebug for this. If the form is submitting nothing or the wrong thing, then check your rendered HTML.
Craig Stuntz
One thought: You did put the button *inside* the {}, right?
Craig Stuntz
My error was to not take out the EndForm call. Controller method firing properly. Answer accepted with many thanks.
KevDog
A: 

If I'm understanding your question properly, you use the html helper and create inputs named:

<%=Html.TextBox("building.FieldNumber1")%>
<%=Html.TextBox("building.FieldNumber2")%>

You should be able to access the data using:

public ActionResult List(Building building)
{
   ...
   var1 = building.FieldNumber1;
   var2 = building.FieldNumber2;
   ...
}

and if your action is to do two different things depending on if form is submitted:

public ActionResult List()
{
    //some code here
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult List(Building building)
{
   ...
   var1 = building.FieldNumber1;
   var2 = building.FieldNumber2;
   ...
}
woopstash
A: 

if anybody is skeptical about the whole 'using' pattern with Html.BeginForm - realize that the IDE is smart enough to match the opening '{' with the ending '}' which makes it very easy to see where your form begins and ends.

Also <% Html.EndForm(); %> requires a semicolon which I'm not sure i like :)

Simon_Weaver