當前位置:
首頁 > 知識 > ASP.NET MVC-樣式和布局

ASP.NET MVC-樣式和布局

添加布局

文件 _Layout.cshtml 表示應用程序中每個頁面的布局。它位於 Views 文件夾中的 Shared 文件夾。

打開文件 _Layout.cshtml,把內容替換成:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8" />

<title>@ViewBag.Title</title>

<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")"></script>

<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")"></script>

</head>

<body>

<ul id="menu">

<li>@Html.ActionLink("Home", "Index", "Home")</li>

<li>@Html.ActionLink("Movies", "Index", "Movies")</li>

<li>@Html.ActionLink("About", "About", "Home")</li>

</ul>

<section id="main">

@RenderBody()

<p>Copyright W3CSchool 2012. All Rights Reserved.</p>

</section>

</body>

</html>

ASP.NET MVC-樣式和布局


HTML 幫助器

在上面的代碼中,HTML 幫助器用於修改 HTML 輸出:

@Url.Content() - URL 內容將在此處插入。

@Html.ActionLink() - HTML 鏈接將在此處插入。

在本教程後面的章節中,您將學到更多關於 HTML 幫助器的知識。



Razor 語法

在上面的代碼中,紅色標記的代碼是使用 Razor 標記的 C#。

@ViewBag.Title - 頁面標題將在此處插入。

@RenderBody() - 頁面內容將在此處呈現。

您可以在我們的 Razor 教程中學習關於 C# 和 VB(Visual Basic)的 Razor 標記的知識。


添加樣式

應用程序的樣式表是 Site.css,位於 Content 文件夾中。

打開文件 Site.css,把內容替換成:

body

{

font: "Trebuchet MS", Verdana, sans-serif;

background-color: #5c87b2;

color: #696969;

}

h1

{

border-bottom: 3px solid #cc9900;

font: Georgia, serif;

color: #996600;

}

#main

{

padding: 20px;

background-color: #ffffff;

border-radius: 0 4px 4px 4px;

}

a

{

color: #034af3;

}

/* Menu Styles ------------------------------*/

ul#menu

{

padding: 0px;

position: relative;

margin: 0;

}

ul#menu li

{

display: inline;

}

ul#menu li a

{

background-color: #e8eef4;

padding: 10px 20px;

text-decoration: none;

line-height: 2.8em;

/*CSS3 properties*/

border-radius: 4px 4px 0 0;

}

ul#menu li a:hover

{

background-color: #ffffff;

}

/* Forms Styles ------------------------------*/

fieldset

{

padding-left: 12px;

}

fieldset label

{

display: block;

padding: 4px;

}

input[type="text"], input[type="password"]

{

width: 300px;

}

input[type="submit"]

{

padding: 4px;

}

/* Data Styles ------------------------------*/

table.data

{

background-color:#ffffff;

border:1px solid #c3c3c3;

border-collapse:collapse;

width:100%;

}

table.data th

{

background-color:#e8eef4;

border:1px solid #c3c3c3;

padding:3px;

}

table.data td

{

border:1px solid #c3c3c3;

padding:3px;

}



ASP.NET MVC-樣式和布局

_ViewStart 文件

Shared 文件夾(位於 Views 文件夾內)中的 _ViewStart 文件包含如下內容:

@{Layout = "~/Views/Shared/_Layout.cshtml";}

這段代碼被自動添加到由應用程序顯示的所有視圖。

如果您刪除了這個文件,則必須向所有視圖中添加這行代碼。

ASP.NET MVC-樣式和布局

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

ASP.NET MVC-控制器
ASP.NET MVC-視圖
ASP.NET MVC-SQL 資料庫
ASP.NET Razor-VB 變數
ASP.NET Razor-VB 循環和數組

TAG:程序員小新人學習 |