Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

State management in ASP.NET MVC

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.

like image 682
Joe Capka Avatar asked Jan 04 '10 16:01

Joe Capka


People also ask

What are the state management techniques in ASP.NET MVC?

In ASP.NET, there are two types of state management: client-side state management and server-side state management. Client-side state management refers to the technique of storing data on the client's browser in the form of either cookies or hidden fields.

What is state management in asp net?

ASP.NET State management is a preserve state control and object in an application because ASP.NET web applications are stateless. A new instance of the Web page class is created each time the page is posted to the server.

Is MVC stateless or stateful?

MVC is stateless because HTTP is. There is nothing in HTTP that indicates when a session starts or ends.

What is state management system?

Overall, state management makes the state of an app visible in the form of a data structure, improving developers' ability to work with the app. State management libraries provide developers with the tools needed to create the data structures and change them when new actions occur.


1 Answers

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") %>
like image 191
kͩeͣmͮpͥ ͩ Avatar answered Sep 18 '22 17:09

kͩeͣmͮpͥ ͩ