views:

161

answers:

3

I've created a PartialView which I render with Html.RenderPartial, passing the name of the view and the strongly-typed data item to bind to (below):

    <% Html.RenderPartial("SearchViewUserControl", ViewData["SearchData"]); %>

The partial view has a form containing a submit button:

<% using (Html.BeginForm("Search", "Home"))
   { %>
             ...
    <div>
        <input type="submit" value="Search" />
    </div>
<% } %>

I've set a breakpoint in my controller's action method (below) but nothing is set in searchData. What am I doing wrong?

   public ActionResult Search(SearchDomain searchData)
    {
        if (ModelState.IsValid)
        {
        }

        return View();
    }
A: 

You need to post the actual form elements for anybody to know whats wrong.

The form html is what sets the binding to SearchDomain. You want to have your form elements named like this:

<input name="searchData.SomeProperty">

For them to bind to your action parameter.

jfar
A: 

In order to pull a SearchDomain object out of your view from a controller method, your view has to either inherit from System.Web.Mvc.ViewPage<Models.SearchDomain>, or a custom ViewModel class that contains a SearchDomain object.

The other way to do it is to have your view inherit from System.Web.Mvc.ViewPage, and use UpdateModel to cast the view data to a SearchDomain object. Something like this:

public ActionResult Save()
{
        SearchDomain domain = new SearchDomain ();

        UpdateModel(domain , new[] { "Name", "Email", "Phone", ... });
        return View(domain); 
}
Robert Harvey
Robert,It makes sense that I would have to inherit from System.Web.Mvc.ViewPage<Models.SearchDomain>, but in this case, I have the search view implemented as a UserControl, which derives from System.Web.Mvc.ViewUserControl<Web.Models.SearchDomain>. Yet the action method is in the controller associated with the page.
bobuva
A: 

To be honest, I think RenderAction is much easier to use.

Robert Harvey