Good Solution
In this section, we will test an alternative and better solution to transfer data from controller behavior to the view master page. Instead of adding a movie category to each controller behavior, we only add the movie category to the view data once. All View data used by the view master page is added to an application controller.
The applicationcontroller class is included in Listing 4.
List4-Controllers \ applicationcontroller. CS
using System.Linq;using System.Web.Mvc;using MvcApplication1.Models;namespace MvcApplication1.Controllers{ public abstract class ApplicationController : Controller { private MovieDataContext _dataContext = new MovieDataContext(); public MovieDataContext DataContext { get { return _dataContext; } } public ApplicationController() { ViewData["categories"] = from c in DataContext.MovieCategories select c; } }}
You need to pay attention to applicationcontroller in Listing 4. First, this class inherits from the system. Web. MVC. controller base class. The application controller is a controller class.
Second, the applicationcontroller class is an abstract class. An abstract class is a class that must be implemented by a specific class. Because applicationcontroller is an abstract class, you cannot call any methods in the class. If you want to directly call the applicationcontroller class, you will get an error message that the resource cannot be found.
Third, applicationcontroller contains a constructor for adding movie categories to view data. Each controller class inherited from the applicationcontroller class automatically calls the constructor of the applicationcontroller class. Movie categories are automatically included in view data whenever you call the behavior of controllers inherited from the applicationcontroller class.
The movies controller in listing 5 inherits from applicationcontroller.
List5-Controllers \ moviescontroller. CS
using System.Linq;using System.Web.Mvc;namespace MvcApplication1.Controllers{ public class MoviesController : ApplicationController { /// <summary> /// Show list of all movies /// </summary> public ActionResult Index() { ViewData["movies"] = from m in DataContext.Movies select m; return View(); } /// <summary> /// Show list of movies in a category /// </summary> public ActionResult Details(int id) { ViewData["movies"] = from m in DataContext.Movies where m.CategoryId == id select m; return View(); } }}
The movies controller, as discussed in the previous section, exposes two behavior methods, index () and detials. Note that the movie category list displayed on the master page is neither added by the index () function nor by the details () function. Because the movies controller inherits from applicationcontroller, the movie category list is automatically added to view data.
Note that the solution for adding View data to a view master page does not violate the dry principle. The code for adding a movie category list to view data only exists in one place: applicationcontroller constructor.
Summary
In this tutorial, we discuss two methods to transfer View data from a controller to a view master page. First, we tested a simple but difficult to maintain method. In section 1, we discussed how to add view data to the view master page in each controller action. Because it violates the DIY principle, we ended the discussion with a solution that is not a good solution.
Next, we tested a better strategy. Unlike adding View data to each controller, we only add view data in applicationcontroller. In this way, you can avoid code duplication.
From: http://hi.baidu.com/aliasmic/blog/item/9bb4c3b5dc5d93798ad4b2ef.html