<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      返璞歸真 asp.net mvc (4) - View/ViewEngine

      [索引頁]
      [源碼下載]


      返璞歸真 asp.net mvc (4) - View/ViewEngine


      作者:webabcd


      介紹
      asp.net mvc 之 View 和 ViewEngine
      • ViewData 和 TempData 都可以向 View 傳遞數據,其中 TempData 是保存在 Session 中的,一次請求后此 Session 會被清除
      • HtmlHelper - 在 View 中顯示 HTML 元素的一個幫助類
      • IViewEngine - 自定義的視圖引擎需要實現此接口
      • VirtualPathProviderViewEngine - 實現了 IViewEngine 接口的抽象類,實現了根據指定的路徑格式搜索對應的頁面文件的功能(內用緩存機制)
      • IView - 只有一個需要實現的方法,就是呈現 HTML 結果


      示例
      1、演示 View 的 Demo
      ViewDemoController.cs
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.Mvc;
      using System.Web.Mvc.Ajax;

      using MVC.Models;

      namespace MVC.Controllers
      {
          
      public class ViewDemoController : Controller
          
      {
              ProductSystem ps 
      = new ProductSystem();

              
      public ActionResult Details(int id)
              
      {
                  var product 
      = ps.GetProduct(id);

                  
      if (product == null)
                  
      {
                      
      return View("NotFound");
                  }

                  
      else
                  
      {
                      product.CategoriesReference.Load();

                      
      // 編輯 Product 的時候需要在一個 DropDownList 中選擇其所對應的 Category, 所以這里要構造一個 SelectList 類型的 ViewData
                      if (product.Categories == null)
                          ViewData[
      "CategoryList"= new SelectList(new CategeorySystem().GetCategory(), "CategoryId""CategoryName");
                      
      else
                          ViewData[
      "CategoryList"= new SelectList(new CategeorySystem().GetCategory(), "CategoryId""CategoryName", product.Categories.CategoryID);

                      
      // ViewData 和 TempData 都可以向 View 傳遞數據,其中 TempData 是保存在 Session 中的,一次請求后此 Session 會被清除
                      
      // 在 View 中使用的時候,ViewData[key] 或 TempData[key] 即可
                      TempData["Temp"= "TempData";

                      
      return View("Details", product);
                  }

              }


              [AcceptVerbs(HttpVerbs.Post)]
              
      public ActionResult Update(int id, FormCollection formValues)
              
      {
                  var product 
      = ps.GetProduct(id);

                  
      // 可以通過 UpdateModel, 讓系統自動為屬性賦值(通過反射的方式,取得對象的屬性名稱,然后和 Request 的 key 做匹配,匹配成功的則賦值)
                  UpdateModel<Products>(product);

                  
      // 通過以下的方式讓 UpdateModel 只更新指定屬性
                  
      // string[] allowedProperties = new[] { "ProductName", "UnitPrice" };
                  
      // UpdateModel(product, allowedProperties);

                  var category 
      = new CategeorySystem().GetCategory(int.Parse(Request.Form["Category"]));
                  product.CategoriesReference.EntityKey 
      = ps.CreateEntityKey("Categories", category);

                  
      if (!product.IsValid)
                  
      {
                      
      foreach (var validation in product.GetValidation())
                      
      {
                          
      // 設置驗證信息
                          ModelState.AddModelError(validation.PropertyName, validation.ErrorMessage);
                      }

                  }

                  
      else
                  
      {
                      ps.Save();
                  }


                  ViewData[
      "CategoryList"= new SelectList(new CategeorySystem().GetCategory(), "CategoryId""CategoryName", category.CategoryID);

                  
      return View("Details", product);
              }

          }

      }

      Details.aspx
      <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MVC.Models.Products>" %>

      <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
          Details
      </asp:Content>
      <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
          
      <style type="text/css">
              .bold
              
      {
                  font-weight
      : bold;
              
      }

          
      </style>
          
      <h2>
              Details
      </h2>
          
      <%= Html.ValidationSummary("輸入信息有誤"%>
          
      <% Html.BeginForm("Update""ViewDemo"new { id = Model.ProductID }, FormMethod.Post); %>
          
      <p>
              
      <strong>ProductID:</strong>
              
      <%= Html.Encode(Model.ProductID) %>
          
      </p>
          
      <p>
              
      <label for="ProductName">
                  ProductName:
      </label>
              
      <%= Html.TextBox("ProductName", Model.ProductName, new { style = "color: blue;", @class = "bold" })%>
              
      <%= Html.ValidationMessage("ProductName""*"%>
          
      </p>
          
      <p>
              
      <label for="Category">
                  Category:
      </label>
              
      <%-- Html.ListBox() 和 Html.DropDownList() 需要 IEnumerable<SelectListItem> 類型的數據做數據源 --%>
              
      <%= Html.DropDownList("Category", ViewData["CategoryList"as SelectList)%>
          
      </p>
          
      <p>
              
      <strong>UnitPrice:</strong>
              
      <%= Html.Encode(string.Format("{0:F2}", Model.UnitPrice))%>
          
      </p>
          
      <p>
              
      <input type="submit" value="Save" />
          
      </p>
          
      <p>
              
      <%= TempData["Temp"]%>
          
      </p>
          
      <% Html.EndForm(); %>
          
      <p>
              
      <%=Html.RouteLink("返回首頁"new { Controller = "Home" })%>
          
      </p>

          
      <%-- 需要使用 Web Form 方式的話,則在后置代碼中繼承 System.Web.Mvc.ViewPage 或 System.Web.Mvc.ViewPage<T> 即可-- %>
          
          
      <%-- 
      HtmlHelper 簡要說明:


      可以用如下的方式生成 form
      using (Html.BeginForm()) { }
      using (Html.BeginRouteForm()) { }
      Html.BeginForm(); Html.EndForm();


      可以使用 Html.ListBox(), Html.RadioButton() 之類的來生成 html 元素


      Html.ValidationMessage() 
      - 指定的 ModelName 輸入信息不合法時所輸出的驗證信息
      Html.ValidationSummary() 
      - 匯總所有驗證信息
      驗證信息可以在 Action 中用 ModelState.AddModelError() 的方式來添加
      驗證信息的樣式通過樣式表修改 .field
      -validation-error{} .input-validation-error {} .validation-summary-errors {}


      Html.Encode(); Html.AttributeEncode(); 用于對輸出的內容做編碼


      Html.RenderPartial() 
      - 引入一個 Partial View


      Html.ActionLink() 
      - 根據 Action 找目標
      Html.RouteLink() 
      - 根據路由找目標


      Html.ViewContext 
      - View 的上下文信息。包括 Controller, TempData, ViewData, 路由信息, HttpContext 等信息
      --
      %>
      </asp:Content>


      2、創建一個自定義的 ViewEngine 的 Demo
      MyView.cs
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;

      using System.Web.Mvc;
      using System.IO;
      using System.Text.RegularExpressions;

      namespace MVC
      {
          
      /// <summary>
          
      /// 自定義的視圖
          
      /// 視圖需要繼承 IView 接口
          
      /// </summary>

          public class MyView : IView
          
      {
              
      // 視圖文件的物理路徑
              private string _viewPhysicalPath;

              
      public MyView(string viewPhysicalPath)
              
      {
                  _viewPhysicalPath 
      = viewPhysicalPath;
              }


              
      /// <summary>
              
      /// 實現 IView 接口的 Render() 方法
              
      /// </summary>

              public void Render(ViewContext viewContext, TextWriter writer)
              
      {
                  
      // 獲取視圖文件的原始內容  
                  string rawContents = File.ReadAllText(_viewPhysicalPath);

                  
      // 根據自定義的規則解析原始內容  
                  string parsedContents = Parse(rawContents, viewContext.ViewData);

                  
      // 呈現出解析后的內容
                  writer.Write(parsedContents);
              }



              
      public string Parse(string contents, ViewDataDictionary viewData)
              
      {
                  
      // 對 {##} 之間的內容作解析
                  return Regex.Replace
                  (
                      contents, 
                      
      @"\{#(.+)#\}"

                      
      // 委托類型 public delegate string MatchEvaluator(Match match)
                      p => GetMatch(p, viewData)
                  );
              }


              
      protected virtual string GetMatch(Match m, ViewDataDictionary viewData)
              
      {
                  
      if (m.Success)
                  
      {
                      
      // 獲取匹配后的結果,即 ViewData 中的 key 值,并根據這個 key 值返回 ViewData 中對應的 value
                      string key = m.Result("$1");
                      
      if (viewData.ContainsKey(key))
                      
      {
                          
      return viewData[key].ToString();
                      }

                  }


                  
      return string.Empty;
              }

          }

      }


      MyViewEngine.cs
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;

      using System.Web.Mvc;

      namespace MVC
      {
          
      // MvcContrib 中提供了很多 ViewEngine, 還提供了以 asp.net mvc 框架為基礎的一些額外的功能
          
      // 地址:http://www.codeplex.com/MVCContrib

          
      /// <summary>
          
      /// 自定義的視圖引擎
          
      /// 視圖引擎需要繼承 IViewEngine 接口
          
      /// VirtualPathProviderViewEngine 繼承了 IViewEngine 接口,實現了根據指定的路徑格式搜索對應的頁面文件的功能(內用緩存機制)
          
      /// </summary>

          public class MyViewEngine : VirtualPathProviderViewEngine
          
      {
              
      public MyViewEngine()
              
      {
                  
      // 自定義 View 路徑格式
                  base.ViewLocationFormats = new string[] 
                  

                      
      "~/Views/{1}/{0}.my""~/Views/Shared/{0}.my" 
                  }
      ;
              }


              
      protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
              
      {
                  
      return this.CreateView(controllerContext, partialPath, string.Empty);
              }


              
      /// <summary>
              
      /// 根據指定路徑返回一個實現了 IView 接口的對象
              
      /// </summary>

              protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
              
      {
                  var physicalPath 
      = controllerContext.HttpContext.Server.MapPath(viewPath);

                  
      return new MyView(physicalPath);
              }

          }

      }


      Global.asax.cs
      protected void Application_Start()
      {
          
      // 增加新的視圖引擎 ViewEngine
          ViewEngines.Engines.Add(new MyViewEngine());  
      }

      CustomViewEngineController.cs
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.Mvc;
      using System.Web.Mvc.Ajax;

      namespace MVC.Controllers
      {
          
      /// <summary>
          
      /// 用于演示自定義的 ViewEngine 的 Controller
          
      /// </summary>

          public class CustomViewEngineController : Controller
          
      {
              
      public ActionResult Index()
              
      {
                  ViewData[
      "name"= "webabcd";
                  ViewData[
      "age"= "70";

                  
      // 如果視圖文件中有 {##} 形式的字符串,則 MyViewEngine 會對其做相應的解析
                  
      // 如 {#name#} 會被解析為 webabcd

                  
      return View();
              }
        
          }

      }


      Index.my(智能感知在“工具 - 選項 - 文本編輯器 - 文件擴展名”中編輯)
      <html>
      <head>
          
      <title>創建自定義的 ViewEngine 的 Demo</title>
      </head>
      <body>
          
      <div>name: {#name#}</div>
          
      <div>age: {#age#}</div>
      </body>
      </html>

      運行結果:
      name: webabcd
      age: 70


      OK
      [源碼下載]
      posted @ 2009-05-14 09:06  webabcd  閱讀(6990)  評論(7)    收藏  舉報
      主站蜘蛛池模板: 野花香视频在线观看免费高清版| 久久这里有精品国产电影网| 国内视频偷拍久久伊人网| 国产不卡一区二区在线视频| 欧美国产精品啪啪| 日本高清在线播放一区二区三区 | 国产精品美女一区二三区| 亚洲男女一区二区三区| 日日碰狠狠添天天爽五月婷| 中国亚州女人69内射少妇| 亚洲中文久久久久久精品国产| 国产伦精品一区二区三区免费迷| 日韩高清免费一码二码三码| 国产高清自产拍av在线| 亚洲精品成人片在线观看精品字幕 | 少妇人妻偷人精品系列| 国产美女69视频免费观看| 亚洲日韩久热中文字幕| 欧美性猛交xxxx乱大交极品| 亚洲国产成人久久精品app| 亚洲精品国产精品国在线| 99热在线观看| 国产漂亮白嫩美女在线观看 | 在线精品国产中文字幕| 亚洲国产精品区一区二区| 性色在线视频精品| 国产成人拍国产亚洲精品| 亚洲欧美日韩成人综合一区| 国产伦精品一区二区三区妓女下载 | 国产精品一区二区 尿失禁| 国产熟女精品一区二区三区| 亚洲日韩精品一区二区三区无码| 亚洲成人av在线系列| 亚洲免费视频一区二区三区| 亚洲精品乱码久久久久久按摩高清| 日韩av在线一卡二卡三卡| 久久久久久亚洲精品a片成人| 萝北县| 国产福利片无码区在线观看| 亚洲综合伊人久久大杳蕉| 偷窥国产亚洲免费视频|