学习MVC第三个参数传递(主要讲ViewModel形式)
1: 控制器传递数据给视频有三种形式的; ViewData(弱类型) TempData (弱类型) ViewModel(强类型)
其中ViewData 只能用在本视图中的! TempData虽然可以跨视图!但也只能一次,超过一次就会被清空!所以是一次性的Session
ViewModel是用模型;下面是视图的顶部!若是用ViewModel就是下面这样的!但一次只能传递一个Model到视图所以要是有多个实体就要把它结合到一个实体里面;
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcAppView1.Models.BlogIndexView>" %>
下面这个是前面两种的视图顶部
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/MyViewMaster.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
2:实例传递Model 且多个实体的
首先是Model层
using System.Web;
namespace MvcAppView1.Models
{
public class BlogIndexView
{
public string Title { get; set; }
public string Mood { get; set; }
public IList<BlogClass> BlogClasses{ get; set; }
public BlogInfo Blog { get; set; }
}
public class BlogInfo
{
public string Title { get; set; }
public string Details { get; set; }
}
public class BlogClass
{
public string Title { get; set; }
public string Explain { get; set; }
}
}
再者就是控制器的代码:
using MvcAppView1.Models;
namespace MvcAppView1.Controllers
{
public class BlogController : Controller
{
public ActionResult Index()
{
BlogIndexView biv = new BlogIndexView()
{
Title = "zhh 的博客",
Mood = "冬天就要来了……好像已经来了……"
};
biv.BlogClasses = new List<BlogClass>(){
new BlogClass(){ Title="dotNET Framework", Explain="我对.NET平台的认知与感悟"},
new BlogClass(){ Title="ASP.NET", Explain="ASP.NET中的一些纠结" },
new BlogClass(){ Title="ASP.NET MVC", Explain="ASP.NET 的新东西——MVC" },
new BlogClass(){ Title="个人笔记", Explain="学习中的一些感悟" }
};
biv.Blog = new BlogInfo() {
Title = "第一个.NET程序",
Details = "<p>其它不说,来看代码:</p><blockquote><p><code>using System;\n\n" +
"namespace mdibb {\n\tclass Class1 {\n\t\tstatic void Main(string[] args) {\n" +
"\t\t\tConsole.WriteLine(\"Hello, World\");\n\t\t}\n\t}\n" +
"}</code></p></blockquote><p>上面代码我向控制台中打印一个行文字“Hello World”</p>" };
return View(biv);
}
}
}
最后是视图的代码:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcAppView1.Models.BlogIndexView>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title><%= Model.Title %></title>
<link href="http://www.cnblogs.com/Content/Blog.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="content">
<div id="search">
<form action="" method="get">
<p>
search: <input type="text" />
<input type="image" src="http://images.cnblogs.com/zoom.jpg" value="Submit" alt="Search" title="Search" />
</p>
</form>
</div>
<div id="header">
<h1><%=Model.Title %></h1>
<p><%=Model.Mood %></p>
</div>
<div id="body">
<div id="links">
<p>
<%foreach (var bc in Model.BlogClasses)
{ %>
<a href="#"><%=bc.Title %></a><br/>
<%=bc.Explain %>
<br/><br/>
<%} %>
</p>
</div>
<div id="main">
<h2><%=Model.Blog.Title %></h2>
<%=Model.Blog.Details %>
</div>
</div>
<div id="footer">
Copyright ?20xx Your Name. All Rights Reserved.<br/>
Design by <a href="http://www.mdibb.net" title="Website of Matt Dibb">Matt Dibb</a> 2006. <a href="http://jigsaw.w3.org/css-validator/check/referer" title="Validate CSS">CSS</a> <a href="http://validator.w3.org/check/referer" title="Validate XHTML">XHTML</a>
</div>
</div>
</body>
</html>