Reputation: 562
I have built an ASP.NET MVC app some time ago, and after a few maintenance cycles am wondering whether I took the best approach to state management. Everything works, but I get the feeling there is a better way.
The site is based on a search functionality that has quite a few options. A user starts using the site by filling in a number of search options in a form and clicking the 'search' button. This button posts to the Search method with all the search options being defined as parameters to the Search methods, eg:
public ActionResult Search(string param1, string param2, string param3, int? param3, long? param4)
Now the results page that shows up has a number of links on it, leading to various detail pages, etc. Since I need the search state to be preserved on the detail page, I find myself creating ActionLinks with lots of parameters all over the place, such as:
<%=Html.ActionLink("LinkText", "MethodName", new {id="idOfDetailPage", param1=Model.param1, param2=Model.param2, param3=Model.param3, param4=Model.param4}, null)%>
Most of the parameter values in each link do not change from the current state of the search, but I need to pass them in order to be able to create other links in the detail page with the current search parameters, such as "back to search results" for instance.
When I need to add a search parameter due to a new feature request, I find myself modifying a lot of links as well as any Controller methods that the links lead to. This is where I feel that I need a better way.
I have thought about using session state for keeping the search parameters, but for some reason thought this was not the best thing to use in ASP MVC and so am curious if there is another, cleaner way to do this.
Note: I have also tried an approach where I use a strongly typed object in the ActionLink but I still need to pass parameters to that object so it doesn't get much better.
Any ideas are appreciated.
Upvotes: 7
Views: 7396
Reputation: 7856
Using session state for this kind of thing is alway a nuisance as it means these pages can't be bookmarked and if you want to have more than one tab open it starts getting messy.
You could create a new SearchParameters class:
public class SearchParameters
{
public string Param1 { get; set; }
public string Param2 { get; set; }
}
modify your Action to be
public ActionResult Search(SearchParameters params)
and then pass this back to the view through the view data.
your view should then be able to use
<%=Html.ActionLink("LinkText", "MethodName", Model) %>
If you're using this all over the place, you might like to create an HtmlHelper Extension:
public static class SearchExtensions
{
public static string SearchLink<TModel>(this HtmlHelper<TModel> helper, string linkText)
where TModel : SearchModel, class //required by ASP.NET MVC
{
return helper.ActionLink(linkText, "MethodName", modelType.ViewData.Model) %>
}
}
and then your search is as simple as:
<%=Html.SearchLink("LinkText") %>
Upvotes: 1