- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
Master Pages in VB.NET
Master Pages Encode QR Code In VB.NET Using Barcode encoder for .NET Control to generate, create Denso QR Bar Code image in VS .NET applications. www.OnBarcode.comRead QR Code 2d Barcode In VB.NET Using Barcode reader for VS .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comIf the view is not a partial view, it can have a master page. The master page in this context is exactly the same as the master page in Web Forms. It s a standard .master file, but it s located under the Views\Shared folder. Generate Barcode In VB.NET Using Barcode encoder for .NET framework Control to generate, create bar code image in .NET applications. www.OnBarcode.comBar Code Recognizer In VB.NET Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in .NET applications. www.OnBarcode.comPart II The Core of ASP.NET MVC
QR Code JIS X 0510 Creator In Visual C#.NET Using Barcode generation for VS .NET Control to generate, create Quick Response Code image in Visual Studio .NET applications. www.OnBarcode.comEncode Quick Response Code In .NET Using Barcode generation for ASP.NET Control to generate, create QR Code 2d barcode image in ASP.NET applications. www.OnBarcode.comIn ASP.NET MVC, a master page is implemented through the services of ViewMasterPage, which is defined as follows: Generate QR Code JIS X 0510 In Visual Studio .NET Using Barcode generator for VS .NET Control to generate, create QR image in .NET framework applications. www.OnBarcode.comGTIN - 12 Creator In VB.NET Using Barcode creation for .NET Control to generate, create Universal Product Code version A image in .NET applications. www.OnBarcode.compublic class ViewMasterPage : MasterPage { public ViewMasterPage(); // Properties public AjaxHelper Ajax { get; } public HtmlHelper Html { get; } public object Model { get; } public TempDataDictionary TempData { get; } public UrlHelper Url { get; } public ViewContext ViewContext { get; } public ViewDataDictionary ViewData { get; } internal ViewPage ViewPage { get; } public HtmlTextWriter Writer { get; } } Painting UCC-128 In VB.NET Using Barcode creator for VS .NET Control to generate, create UCC.EAN - 128 image in .NET applications. www.OnBarcode.comPaint Bar Code In Visual Basic .NET Using Barcode generator for .NET Control to generate, create barcode image in VS .NET applications. www.OnBarcode.comAs you can see, it extends the ASP.NET MasterPage class with the typical helpers and properties of ASP.NET MVC views, such as Html, Model, and ViewContext. By default, a master page in ASP.NET MVC doesn t require a code-behind class. However, if you need to expose your own programming model out of the master, you can use a <script> server-side tag (which is the recommended approach) or manually create code-behind classes. Here s a brief example: Linear Drawer In VB.NET Using Barcode drawer for .NET Control to generate, create 1D image in .NET applications. www.OnBarcode.comGenerate Royal Mail Barcode In Visual Basic .NET Using Barcode maker for VS .NET Control to generate, create British Royal Mail 4-State Customer Code image in VS .NET applications. www.OnBarcode.compublic partial class SiteMasterExtended : System.Web.Mvc.ViewMasterPage { public string PageHeading { get { return this.__PageHeading.Text; } set { this.__PageHeading.Text = value; } } } Making UPC-A In None Using Barcode maker for Online Control to generate, create UPC-A Supplement 5 image in Online applications. www.OnBarcode.comEAN13 Generator In Visual C# Using Barcode generation for .NET Control to generate, create European Article Number 13 image in .NET applications. www.OnBarcode.comThe sample master page class inherits from ViewMasterPage and just adds some properties. Most of the time, extra properties are mere wrappers around some of the controls embedded in the master page template, as shown in the following example: Recognizing Bar Code In Java Using Barcode decoder for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comPrinting Code-39 In None Using Barcode drawer for Font Control to generate, create Code 39 image in Font applications. www.OnBarcode.com<div> <asp:Literal runat="server" ID="__PageHeading">The Book</asp:Literal> </div>
Linear Barcode Maker In .NET Using Barcode encoder for Visual Studio .NET Control to generate, create Linear image in VS .NET applications. www.OnBarcode.comPaint Bar Code In Java Using Barcode creation for Android Control to generate, create bar code image in Android applications. www.OnBarcode.comNote that if you decide to add a code-behind class manually, you should ensure that Visual Studio also creates a designer class file (xxx.master.designer.cs) that includes references to server controls in the markup: UPC Symbol Maker In .NET Framework Using Barcode generator for Reporting Service Control to generate, create UCC - 12 image in Reporting Service applications. www.OnBarcode.comDraw QR In Objective-C Using Barcode drawer for iPad Control to generate, create QR-Code image in iPad applications. www.OnBarcode.compublic partial class SiteMasterExtended { protected global::System.Web.UI.WebControls.Literal __PageHeading; ... } 5 Inside Views
To set properties exposed by the master view, you need to write a handler for the PreInit event in the page life cycle, as shown here: <%@ Page MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> <%@ MasterType TypeName="ProgMvc.Views.Shared.SiteMasterExtended" %> <script runat="server" Language="C#"> protected void Page_PreInit(object sender, EventArgs e) { this.Master.PageHeading = "s"; } </script> <asp:Content ...> ... </asp:Content> In Web Forms, through a PreInit handler you could also switch master pages on the fly, as shown below. protected void Page_PreInit(object sender, EventArgs e) { this.MasterPageFile = "~/Views/Shared/VertLayout.Master"; } In ASP:NET MVC, you don t need this if you want to be able to switch master pages on the fly and based on runtime conditions. Because the generation of the view is distinct process in ASP.NET MVC, all you need to do is tell the view engine which master page it has to use. public ActionResult Index() { ... return View("Index", "SiteMaster"); } You do that simply using a different overload of the controller s View method.
Strongly Typed Views
In ASP.NET MVC, any view is expected to be isolated from the controller code. The view should receive from the outside world any data it has to process. Data can be passed in two nonexclusive ways: via the ViewData dictionary and via an object model. As mentioned, ViewData is an object of type ViewDataDictionary. Any data you store in a dictionary is treated as an object and requires casting, boxing, or both in order to be worked on. A dictionary is definitely not something you would call strongly typed. At the same time, a dictionary is straightforward to use and works just fine. Part II The Core of ASP.NET MVC
ViewDataDictionary is kind of unique because it also features a few ASP.NET MVC specific properties such as the Model, ModelState, and ModelMetadata properties, as shown here: public class ViewDataDictionary : IDictionary<string, object>, ICollection<KeyValuePair<string, object>>, IEnumerable<KeyValuePair<string, object>>, IEnumerable { public object Model { get; set; } public ModelStateDictionary ModelState { get; } public virtual ModelMetadata ModelMetadata { get; set; } ... } The ModelState property gets information about the state of the model. It typically contains entries describing what s wrong, if anything, in the data being worked on in the view. The ModelMetadata property, instead, stores information about the data being processed by the view the model. Metadata includes display and edit information about properties of the model. Metadata information is obtained from a metadata provider. The default provider is based on the Data Annotations library. (See 6, Inside Models, and 7, Data Entry in ASP.NET MVC, for more details.) The Model property is an alternative and object-oriented way of passing data to the view object. Instead of fitting flat data into a dictionary, you can shape a custom object to faithfully represent the data the view expects. The Model property just gives you a chance to create a view-model object that is unique for each view. If you intend to use the Model property to pass data to the view, you have to make it explicit, as shown here: public partial class YourPage : ViewPage<YourViewModel> { ... } The view page class derives from ViewPage<T> instead of ViewPage. If you don t use a code-behind class, you achieve the same goal with the following page directive in the view file: <%@ Page MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<YourViewModel>" %> The ViewData dictionary is good enough for quick-and-dirty or short-lived sites. However, it becomes inadequate as the complexity of the view (and the number of views) grows beyond a certain threshold. So what should you do
|
|