views:

55

answers:

2

Hi.

I'm trying to create a page that contains a grid and searching. The issue is that I want to have a partial view for the grid and one for the searching.

If doing a search, this should render the grid partial view with the new information.

At the moment I need information, such as what column I'm sorting by and so on, from the grid (currently stored in viewdata), in order to do the search as I want to keep those settings. This information is only available in the grid partial though.

What's the best approach of this to make it neat and nice in the code, but not a mess to work with?

Where can I store information that I need in the other partial view?

Partial View 1;

<table>
       <%= Html.CreateGrid(Model, "Grid", "Grid", (int)ViewData["SortColumn"], (bool)ViewData["SortedASC"])%>
</table>

Partial View 2;

<div class="searchControl">
    <input type="text" class="SearchBox" href="<%= Url.Action("Grid", "Grid", new {page = 1, columnToSortBy=/* would like to access viewdata from partial view 1 here. */, sortASC = /* would like to access viewdata from partial view 1 here. */  } ) %>" />
    <input type="submit" value="Search" class="SearchButton" />
</div>

I know I might take the completely wrong approach on this, so feel free to point me in the right one!

Thanks!

+1  A: 

ViewData is a good place to store data that is accessed in Views and Partials.

Even better if you use strongly typed views. Then you could access the data for sorting an filtering via a typed model. I would have the model-classes implement an interface IGridFeatures that has properties for SortedASC, SortColumn, Page.

Its often a good idea to have these optional properties not in the route but in a querystring.

Malcolm Frexner
Okay, so if I create properties in the view, and then when loading the partial view I'm setting the values, that should be okay?
MrW
You pass a Model to the view. The view has access to the properties of the Model. Maybe this blog helps http://stephenwalther.com/blog/archive/2009/04/13/asp.net-mvc-tip-50-ndash-create-view-models.aspx.
Malcolm Frexner
A: 

I think you'll be better of controlling your link through javascript, since all you really want is to control the UI.

HeavyWave
But I can't do this as it sends the request to the server and then after that render the partial view from scratch. And I don't have the information I need to send the request at this point, as that information is in the viewdata from partial view 1.
MrW