ASP. net mvc tutorial-transfer data to view master page a.m.
Address: http://www.asp.net/learn/mvc/tutorial-13-cs.aspx
Transfer Data to the master page
The purpose of this tutorial is to explain how you can pass data from a controller to a view master page. We tested two policies for transferring data to the master page. First, we discussed an easy solution, but this solution made the application difficult to maintain. Next, we tested a better solution. It requires a little more early work, but it will produce applications that are easier to maintain.
Problem
Imagine that you are creating a movie database application, and then you want to display a movie category list (1) on every page of the application ). In addition, this category table is stored in the database table. Therefore, it makes sense to retrieve these categories from the database and then display these movie categories on a view master page.
Figure1:Display movie categories on the view master page
The problem arises. On the view master page, how do you retrieve the category list of movies? The method for directly calling the model class on the master page is obviously attractive. In other words, it is tempting to search for database data on your template page. However, bypassing your MVC controller to obtain access to the database violates the clean separation of concerns-one of the major advantages of this MVC application.
In an MVC application, you will want the MVC controller to process the exchange between all MVC views and MVC models. This separation of focus means a more maintainable, adaptable, and tested application.
In an MVC application, all data is transmitted to a view, including the view master page, which should be transmitted to the view by the Controller action. In addition, data should be transmitted using the advantages of View data. In the rest of this tutorial, I tested two ways to pass data to the view master page.
Simple Solution
Let's start with a simple solution. This simple solution is to transfer View data to the master page in each controller action.
Consider the Controller in Listing 1. It exposes two behaviors, index () and detials. The index () behavior method returns all movies in the movies database table. The details () behavior method returns all movies of a specific category.
List1-Controllers \ homecontroller. CS
using System.Linq;using System.Web.Mvc;using MvcApplication1.Models;namespace MvcApplication1.Controllers{ [HandleError] public class HomeController : Controller { private MovieDataContext _dataContext = new MovieDataContext(); /// <summary> /// Show list of all movies /// </summary> public ActionResult Index() { ViewData["categories"] = from c in _dataContext.MovieCategories select c; 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["categories"] = from c in _dataContext.MovieCategories select c; ViewData["movies"] = from m in _dataContext.Movies where m.CategoryId == id select m; return View(); } }}
Note that both the index () and details () actions add two items to the view data. The index () Action adds two keys: Categories and movies. The categories key. The movies key represents the list of movies that are also displayed in the index view.
The details () action also adds two keys named categories and movies. The categories key, once again, represents the category of movies displayed on the view master page. The movies key represents a specific category displayed on the details view page (2 ).
Figure2: Details View
The index view is included in Listing 2. It simply iterates the list of movies represented by the movies item in the View data.
List2-Views \ home \ index. aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="MvcApplication1.Views.Home.Index" %><%@ Import Namespace="MvcApplication1.Models" %><asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<ul><% foreach (var m in (IEnumerable<Movie>)ViewData["movies"]) { %> <li><%= m.Title %></li><% } %></ul></asp:Content>
The view master page is included in listing 3. The view master page iterates and renders all movie categories represented by the categories item obtained from the View data.
List3-Views \ shared \ site. Master
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.Master.cs" Inherits="MvcApplication1.Views.Shared.Site" %><%@ Import Namespace="MvcApplication1.Models" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
All data is transmitted to the view and view master pages through View data. This is the correct way to pass data to the master page.
So where are the problems with this solution? The problem is that this solution violates the dry (don't repeat yourself) principle. For each controller action, the same movie category list must be added to the view data. Repeated code in the application makes it more difficult for your application to maintain, adapt, and modify.
From: http://hi.baidu.com/aliasmic/blog/item/9bb4c3b5dc5d93798ad4b2ef.html