Wednesday, July 28, 2010

ASP.NET MVC Tips

Adding Links between pages

Our Store Index page lists Genres as plain text. We can easily convert those to dynamic links to our Browse page, so clicking on Disco will navigate to /Store/Browse?genre=Disco. We could just hardcode those links, as follows:
?


That works, but it could lead to trouble later since it relies on a hardcoded string. For instance, if we wanted to rename the Controller, we'd need to search through our code looking for links that need to be updated.

Instead, we can make use of an HTML Helper method. ASP.NET MVC includes HTML Helper methods which are available from our View template code to perform a variety of utility tasks just like this. You'll use the Html.ActionLink helper method pretty often, since it makes it easy to build links and takes care of annoying details like making your paths are properly URL encoded.

Html.ActionLink has several different overloads to allow specifying as much information as you need for your links. In the simplest case, you'll supply just the link text and the Action method. For example, we can link to "/Store/" on the Store Details page with the link text "Go to the Store Index" using the following call:
?
<%: Html.ActionLink("Go to the Store Index", "Index")%>

Note: In this case, we didn't need to specify the controller name because we're just linking to another action within the same controller that's rendering the current view.


Source :- -http://www.asp.net/mvc/tutorials/mvc-music-store-part-2

No comments: