免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項超值服

開通VIP
自定義MVC視圖引擎ViewEngine 創(chuàng)建Model的專屬視圖

MVC內(nèi)置的視圖引擎有WebForm view engine和Razor view engine,當(dāng)然也可以自定義視圖引擎ViewEngine。本文想針對某個Model,自定義該Model的專屬視圖。

□ 思路

1、控制器方法返回ActionResult是一個抽象類
2、ActionResult的其中一個子類ViewResult,正是她使用IView實例最終渲染出視圖
3、需要自定義IView
4、IViewEngine管理著IView,同時也需要自定義IViewEngine
5、自定義IViewEngine是需要全局注冊的

□ IView接口

public interface IView
{
    void Render(System.Web.Mvc.ViewContext viewContext, System.IO.TextWriter writer)
}

第一個參數(shù)ViewContext包含了需要被渲染的信息,被傳遞到前臺的強(qiáng)類型Model也包含在其中。
第二個參數(shù)TextWriter可以幫助我們寫出想要的html格式。

□ IViewEngine接口

public interface IViewEngine
{
    System.Web.Mvc.ViewEngineResult FindPartialView(System.Web.Mvc.ControllerContext controllerContext, string partialViewName, bool useCache);
    System.Web.Mvc.ViewEngineResult FindView(System.Web.Mvc.ControllerContext controllerContext, string viewName, string masterName, bool useCache);
    void ReleaseView(System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.IView view);
}

FindPartialView:在當(dāng)前控制器上下文ControllerContext中找到部分視圖。
FindView:在當(dāng)前控制器上下文ControllerContext中找到視圖。
ReleaseView:釋放當(dāng)前控制器上下文ControllerContext中的視圖。

 

模擬一個Model和數(shù)據(jù)服務(wù)類

   public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Score { get; set; }
    }
 
    public class DataAccess
    {
        List<Student> students = new List<Student>();
 
        public DataAccess()
        {
            for (int i = 0; i < 10; i++)
            {
                students.Add(new Student
                {
                    Id = i + 1,
                    Name = "Name" + Convert.ToString(i+1),
                    Score = i + 80
                });
            }
        }
 
        public List<Student> GetStudents()
        {
            return students;
        }
    }
 

 

實現(xiàn)IView接口,自定義輸出html格式

using System.Collections.Generic;
using System.Web.Mvc;
using CustomViewEngine.Models;
 
namespace CustomViewEngine.Extension
{
    public class StudentView : IView
    {
 
        public void Render(ViewContext viewContext, System.IO.TextWriter writer)
        {
            //從視圖上下文ViewContext中拿到model
            var model = viewContext.ViewData.Model;
            var students = model as List<Student>;
 
            //自定義輸出視圖的html格式
            writer.Write("<table border=1><tr><th>編號</th><th>名稱</th><th>分?jǐn)?shù)</th></tr>");
            foreach (Student stu in students)
            {
                writer.Write("<tr><td>" + stu.Id + "</td><td>" + stu.Name + "</td><td>" + stu.Score + "</td></tr>");
            }
            writer.Write("</table>");
        }
    }
}
 

 

實現(xiàn)IViewEngine,返回自定義IView

using System;
using System.Web.Mvc;
 
namespace CustomViewEngine.Extension
{
    public class StudentViewEngine : IViewEngine
    {
 
        public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
        {
            throw new System.NotImplementedException();
        }
 
        public ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            if (viewName == "StudentView")
            {
                return new ViewEngineResult(new StudentView(), this);
            }
            else
            {
                return new ViewEngineResult(new String[]{"針對Student的視圖還沒創(chuàng)建!"});
            }
        }
 
        public void ReleaseView(ControllerContext controllerContext, IView view)
        {
            
        }
    }
}
 

 

HomeController

using System.Web.Mvc;
using CustomViewEngine.Models;
 
namespace CustomViewEngine.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var students = new DataAccess().GetStudents();
            ViewData.Model = students;
            return View("StudentView");
        }
 
    }
}
 

 

全局注冊

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
 
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
 
            ViewEngines.Engines.Add(new StudentViewEngine());
        }
    }

瀏覽/Home/Index效果:

分類: MVC
標(biāo)簽: MVC, 自定義視圖引擎ViewEngine, 自定義視圖View
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
我喜歡ASP.NET的MVC因為它牛逼的9大理由
ASP.NET MVC的View是如何被呈現(xiàn)出來的?[設(shè)計篇]
ASP.NET MVC4 IN ACTION學(xué)習(xí)筆記
.net MVC通俗易懂實例
Apache OFBIZ快速上手(二)
Asp.Net MVC3 簡單教程(二)詳解Asp.Net MVC3項目
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服