自定义MVC视图引擎ViewEngine 创建Model的专属视图
MVC内置的视图引擎有WebForm view engine和Razor view engine,当然也可以自定义视图引擎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)
}
第一个参数ViewContext包含了需要被渲染的信息,被传递到前台的强类型Model也包含在其中。
第二个参数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:在当前控制器上下文ControllerContext中找到部分视图。
FindView:在当前控制器上下文ControllerContext中找到视图。
ReleaseView:释放当前控制器上下文ControllerContext中的视图。
模拟一个Model和数据服务类
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;
}
}
实现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>分数</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>");
}
}
}
实现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的视图还没创建!"});
}
}
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视图引擎ViewEngine 创建Model的专属视图的更多相关文章
- ASP.NET MVC自定义视图引擎ViewEngine 创建Model的专属视图
MVC内置的视图引擎有WebForm view engine和Razor view engine,当然也可以自定义视图引擎ViewEngine. 本文想针对某个Model,自定义该Model的专属视图 ...
- (翻译)为你的MVC应用程序创建自定义视图引擎
Creating your own MVC View Engine For MVC Application 原文链接:http://www.codeproject.com/Articles/29429 ...
- MVC ViewEngine视图引擎解读及autofac的IOC运用实践
MVC 三大特色 Model.View.Control ,这次咱们讲视图引擎ViewEngine 1.首先看看IViewEngine接口的定义 namespace System.Web.Mvc { ...
- 自定义视图引擎,实现MVC主题快速切换
一个网站的主题包括布局,色调,内容展示等,每种主题在某些方面应该或多或少不一样的,否则就不能称之为不同的主题了.每一个网站至少都有一个主题,我这里称之为默认主题,也就是我们平常开发设计网站时的一个固定 ...
- MVC 【Razor 视图引擎】基础操作 --页面跳转,传值,表单提交
ASPX 与 Razor 仅仅是视图不一样. 新建项目----ASP.NET MVC 4 Web 应用程序------选择模板(空).视图引擎(Razor ) 1.视图中 c# 代码 与 HT ...
- 返璞归真 asp.net mvc (4) - View/ViewEngine
原文:返璞归真 asp.net mvc (4) - View/ViewEngine [索引页] [源码下载] 返璞归真 asp.net mvc (4) - View/ViewEngine 作者:web ...
- Razor视图引擎 语法学习(一)
ASP.NET MVC是一种构建web应用程序的框架,它将一般的MVC(Model-View-Controller)模式应用于ASP.NET框架: ASP.NET约定优于配置:基本分为模型(对实体数据 ...
- 移除apsx视图引擎,及View目录下的web.config的作用
<> 使用Rezor视图引擎的时候移除apsx视图引擎 Global.asax文件 using System; using System.Collections.Generic; usin ...
- (005)每日SQL学习:关于物化视图的一系列创建等语句
--给用户授权 GRANT CREATE MATERIALIZED VIEW TO CDR; --创建物化视图的表日志(具体到某个表,物化视图中用到几个表就需要建立几个日志):当用FAST选项创建物化 ...
随机推荐
- wordpress 常用函数-wpdb类
与数据库建立接口 WordPress为用户提供了一系列用于数据库操作的函数类——wpdb.Wpdb类建立在Justin Vincent编写并维护的ezSQL类的基础上. 使用须知 不可直接调用wpdb ...
- progressDialog和子线程模拟显示拷贝进度
package com.example.wang.myapplication; import android.app.ProgressDialog; import android.os.Bundle; ...
- CCF CSP 201509-2 日期计算
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201509-2 日期计算 问题描述 给定一个年份y和一个整数d,问这一年的第d天是几月几日? ...
- 【LOJ】#2541. 「PKUWC2018」猎人杀
题解 一道神仙的题>< 我们毙掉一个人后总的w的和会减少,怎么看怎么像指数算法 然而,我们可以容斥-- 设\(\sum_{i = 1}^{n} w_{i} = Sum\) 我们把问题转化一 ...
- 微信JS-SDK之图像接口开发详解
由于现在手头的项目中有一个上传证件照认证的功能(手机端),之前的思路是直接点击上传,然后直接将图片上传到服务器去,这篇文章有讲到(http://www.cnblogs.com/it-cen/p/453 ...
- 洛谷P4623 [COCI2012-2013#6] BUREK [模拟]
题目传送门 BUREK 格式难调,题面就不放了. 分析: 一道比较有思维难度的模拟题. 首先我们可以想到,对于一个三角形,可以画出一个最小矩形使得这个三角形被完全包围,并且这个矩形的边平行于坐标轴(图 ...
- HDU - 3577 Fast Arrangement 线段树
Fast Arrangement Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- CSUOJ 1005 Binary Search Tree analog
Description Binary Search Tree, abbreviated as BST, is a kind of binary tree maintains the following ...
- 解问 lambda表达式
目录 1.0 何为Lambda 1.1 Lambda语法特征 1.2 Lambda实例 1.3 Lambda中的stream 1.4 Lambda 中的 stream 效率 1.0 何为Lambda ...
- shell 统计行数
语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同时也给出所有指定文件的总统计数.字是由空格字符区分开的最大字符串. 该命令各选 ...