自定义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选项创建物化 ...
随机推荐
- Nginx 虚拟主机 VirtualHost 配置
Nginx 是一个轻量级高性能的 Web 服务器, 并发处理能力强, 对资源消耗小, 无论是静态服务器还是小网站, Nginx 表现更加出色, 作为 Apache 的补充和替代使用率越来越高. 我在& ...
- 如何在k8s集群里快速运行一个镜像?
在docker里,快速run一个镜像,很简单的. k8s的世界,与之类似. 但要注意一下,如果镜像本身没有提供command命令,这个容器由于前台输出完成,很快就退出了. 所以,遇到这种镜像,就最好自 ...
- Opencv算法运行时间
使用getTickCount() 需要导入命名空间cv,using namespace cv; double t = getTickCount(); funciont(); double tm = ( ...
- 安装配置SVN
官网下载地址,下载完之后如果想看到中文,可以下载语言包进行安装,安装之后TortoiseSVN -> Settings -> General -> Language选项中选择:中文( ...
- [翻译]Gulp.js简介
我们讨论了很多关于怎么减少页面体积,提高重网站性能的方法.有些是操作是一劳永逸的,如开启服务器的gzip压缩,使用适当的图片格式,或删除一些不必要的字符.但有一些任务是每次工作都必须反复执行的.如 新 ...
- forms.ModelForm 与 forms.Form
1. 首先 两者都是forms里的常用类. 2. 这两个类在应用上是有区别的.一般情况下,如果要将表单中的数据写入数据库或者修改某些记录的值,就要让表单类继承ModelForm; 如果提交表单后 不会 ...
- JSP的学习三(中文乱码)
1). 在 JSP 页面上输入中文, 请求页面后不出现乱码: 保证 contentType="text/html; charset=UTF-8", pageEncoding=&qu ...
- MySQL集群原理详解
1. 为什么需要分布式数据库2. MySQL Cluster原理3. MySQL Cluster的优缺点4. MySQL Cluster国内应用5. 参考资料 1. 为什么需要分布式数据库 随着计算机 ...
- PIPESTATUS 对于ksh 无效
BASH SHELL中,通常使用 $? 来获取上一条命令的返回码. 对于管道中的命令,使用$?只能获取管道中最后一条命令的返回码,例如 下面的例子中/not/a/valid/filename是一个不存 ...
- Jquery的方法(二)
一.文档操作1.html()和text()的区别 <div id="J_div"><b><i>我是谁</i></b>&l ...