ASP.NET MVC 页面模块编程语法小结
1.@RenderSection("XXX") 与 @section XXX{}
_Layout.cshtml
<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title</title>
@RenderSection("Header")
</head>
<body>
@RenderBody()
</body>
</html>
Index.cshtml
@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } @section Header{ <link href="@Url.Content("~/Scripts/uploadify-v3.1/uploadify.css")" rel="stylesheet" type="text/css" /> <script src="@Url.Content("~/Scripts/jquery-1.8.1.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/uploadify-v3.1/jquery.uploadify-3.1.min.js")" type="text/javascript"></script> <script type="text/javascript"> $(function () { $('#file_upload').uploadify({ 'swf' : '@Url.Content("~/Scripts/uploadify-v3.1/uploadify.swf")', 'uploader' : '/Home/Upload' }); }); </script> <style type="text/css"> body { font-size: 12px; } .tip { height: 20px; border-bottom: 1px solid #CCC; margin-bottom: 10px; } </style> } <div class="tip">
2.@{Html.RenderAction("Action", "Controller");}
@{Html.RenderAction("GenreNavigationMenu", "System");}
1).Html.RenderAction()方法往往需要和对应的[ChildActionOnly]属性action方法以及对应的局部chtml模板配合使用,在action方法中常使用return PartialView()方法,通常都被用来显示一个功能相对独立的“块”,比如说显示菜单或者导航条.
2).允许我们在chtml中可以直接调用某一个Action,并把返回的结果直接显示在当前调用的View中。
3).return PartialView("TagCloud", tagData);//TagCloud是一个简单的PartialView文件而已
3.@Html.Partial("_TopNavbar")
用于在_Layout.cshtml中组装静态模板页面块