ASP.NET 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 = ; i < ; i++)
{
students.Add(new Student
{
Id = i + ,
Name = "Name" + Convert.ToString(i+),
Score = i +
});
}
} 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 可见效果
ASP.NET MVC自定义视图引擎ViewEngine 创建Model的专属视图的更多相关文章
- 自定义MVC视图引擎ViewEngine 创建Model的专属视图
MVC内置的视图引擎有WebForm view engine和Razor view engine,当然也可以自定义视图引擎ViewEngine.本文想针对某个Model,自定义该Model的专属视图. ...
- ASP.NET MVC 自定义Razor视图WorkContext
概述 1.在ASP.NET MVC项目开发的过程中,我们经常需要在cshtml的视图层输出一些公用信息 比如:页面Title.服务器日期时间.页面关键字.关键字描述.系统版本号.资源版本号等 2.普通 ...
- ASP.NET MVC 5 学习教程:创建连接字符串
原文 ASP.NET MVC 5 学习教程:创建连接字符串 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符串 ...
- ASP.NET MVC自定义验证Authorize Attribute(包含cookie helper)
前几天Insus.NET有在数据库实现过对某一字段进行加密码与解密<使用EncryptByPassPhrase和DecryptByPassPhrase对MS SQLServer某一字段时行加密和 ...
- ASP.NET MVC 5 - 验证编辑方法(Edit method)和编辑视图(Edit view)
在本节中,您将验证电影控制器生成的编辑方法(Edit action methods)和视图.但是首先将修改点代码,使得发布日期属性(ReleaseDate)看上去更好.打开Models \ Movie ...
- asp.net mvc 自定义pager封装与优化
asp.net mvc 自定义pager封装与优化 Intro 之前做了一个通用的分页组件,但是有些不足,从翻页事件和分页样式都融合在后台代码中,到翻页事件可以自定义,再到翻页和样式都和代码分离, 自 ...
- ASP.NET MVC 5 学习教程:Edit方法和Edit视图详解
原文 ASP.NET MVC 5 学习教程:Edit方法和Edit视图详解 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 ...
- ASP.NET MVC 5 学习教程:控制器传递数据给视图
原文 ASP.NET MVC 5 学习教程:控制器传递数据给视图 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字 ...
- 【译】ASP.NET MVC 5 教程 - 7:Edit方法和Edit视图详解
原文:[译]ASP.NET MVC 5 教程 - 7:Edit方法和Edit视图详解 在本节中,我们继续研究生成的Edit方法和视图.但在研究之前,我们先将 release date 弄得好看一点.打 ...
随机推荐
- codeforces-574B
题目连接:http://codeforces.com/contest/574/problem/B B. Bear and Three Musketeers time limit per test 2 ...
- Python的功能模块[2] -> abc -> 利用 abc 建立抽象基类
abc模块 / abc Module 在定义抽象方法时,为了在初始化阶段就检测是否对抽象方法进行了重定义,Python 提供了 abc 模块. from abc import ABCMeta, abs ...
- [Usaco2010 Feb]Chocolate Buying
题目描述 贝西和其他奶牛们都喜欢巧克力,所以约翰准备买一些送给她们.奶牛巧克力专卖店里 有N种巧克力,每种巧克力的数量都是无限多的.每头奶牛只喜欢一种巧克力,调查显示, 有Ci头奶牛喜欢第i种 ...
- Unique Word Abbreviation -- LeetCode
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- 解决android客户端使用soap与服务器通讯错误415
在编写一个android client与服务器使用soap通讯,虽然能连上但不是正常的200代码,而是415,经查询是"HTTP 415 错误 – 不 支持的媒体类型(Unsupported ...
- selenium 截图
http://blog.csdn.net/u010953692/article/details/78320025 # coding:utf-8 # coding:cp936 from selenium ...
- linux的dd命令详解
一.dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 块大小可以使用的计量表 参数注释: 1. if=文件名:输入文件名,缺省为标准输入stdin.即指定源文件.< ...
- Spring整合Hibernate的时候使用hibernate.cfg.xml
Spring整合Hibernate其实也就是把Hibernate的SessionFactory对象封装成:org.springframework.orm.hibernate3.LocalSession ...
- ES6里关于模板字面量的拓展
JS 的字符串相对其他语言来说功能总是有限的,事实上,ES5中一直缺乏许多特性,如多行字符串.字符串格式化.HTML转义等.ES6通过模板字面量的方式进行了填补,模板字面量试着跳出JS已有的字符串体系 ...
- C#字符串操作大全
===============================字符串基本操作================================ 一.C#中字符串的建立过程 例如定义变量 strT=&qu ...