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 弄得好看一点.打 ...
随机推荐
- Usage of API documented as @since 1.6+
报错:即方法是Java1.6才开始有的 File ->Project Structure->Project Settings -> Modules -> Language Le ...
- SenseTime Ace Coder Challenge 暨 商汤在线编程挑战赛*
题目链接 Problems Problem A Problem B bitset…… Problem C Problem D Problem E Problem F Problem G 考虑最小生 ...
- 使用after伪类,配合IE的zoom或者overflow清除浮动
用after伪类实现,兼容多种浏览器:.clearfix:after {content:"."; display:block; height:0; clear:both; visi ...
- 使用create-react-app命令创建一个项目, 运行npm run eject报错
解决方法: 先 git add . 然后 git commit -m ‘init’ 然后再npm run eject
- Codeforces 920 E Connected Components?
Discription You are given an undirected graph consisting of n vertices and edges. Instead of giving ...
- 每天一个linux命令9之crontab 定时任务
crond 是linux用来定期执行程序的命令.当安装完成操作系统之后,默认便会启动此 任务调度命令.crond命令每分锺会定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作. 1语法 ...
- sql server mvp 發糞塗牆
http://blog.csdn.net/dba_huangzj/article/details/38295753
- 使用Chrome DevTools直接调试Node.js与JavaScript(并行)
Good News: 现在我们可以用浏览器调试node.js了!!! 前提 Node.js 6.3+, 这个可上Node.js官网自行下载: Chrome 55+. 如果您本地的chrome升级到最新 ...
- Nand flash uboot 命令详解
转:http://blog.chinaunix.net/uid-14833587-id-76513.html nand info & nand device 显示flash的信息: DM365 ...
- php设计模式之建造者模式
建造者模式 建造者设计模式的目的是消除其他对象的复杂创建过程.使用建造者设计模式不仅是最佳的做法,而且在摸个对象的构造和配置方法改变时候,可以尽可能的减少重复更改代码. <?php /** *p ...