Jason
Jason

Reputation: 11615

MVC2 Partial View, Search Control - Design / Implementation

I have a partial view that is a search textbox and button.

I am trying to make it a partial view so that I can render it in several places. When the user clicks search I want it to redirect to /Home/Search which will show a list of items based on what was put into the search box.

Is this the right approach or should I make the form just run the /Home/Search Index() action and not have a partial view controller at all?

Partial View

    <% using (Html.BeginForm("Search", "SearchBox")) {%>
                <%: Html.TextBoxFor(model => model.searchTerm) %>
                <input type="submit" value="Search" />
    <% } %>

Partial View Controller

  public class SearchBoxController : Controller
    {
        public ActionResult Search(ViewModels.SearchViewModel item)
        {
            Models.DataClasses1DataContext db = new Models.DataClasses1DataContext();
            List<Models.Item> retVal = (from p in db.Items
                                        where p.Name.Contains(item.searchTerm)
                                        select p).ToList();
            return RedirectToAction("Search", "Home"); //No data... What to do????
        }
    }

Upvotes: 2

Views: 1064

Answers (1)

eglasius
eglasius

Reputation: 36037

or should I make the form just run the /Home/Search Index() action and not have a partial view controller at all?

Definitely yes. That's all, there is no real need for the partial controller, specially if it involves an extra redirect.

Upvotes: 2

Related Questions