我是新來的MVC,我面臨一個問題,我怎麼能連接一個控制器「HelloWorld」viewMasterPage「索引」..如果我想我所有的頁面包含組件我需要使用viewMasterPage所有視圖?我如何鏈接viewMasterPage與控制器
編輯 我使用MVC3和剃刀作爲視圖引擎
我是新來的MVC,我面臨一個問題,我怎麼能連接一個控制器「HelloWorld」viewMasterPage「索引」..如果我想我所有的頁面包含組件我需要使用viewMasterPage所有視圖?我如何鏈接viewMasterPage與控制器
編輯 我使用MVC3和剃刀作爲視圖引擎
這樣做的。我假設你正在使用Web窗體視圖引擎。
您的母版頁是您的Views/Shared文件夾中的site.aspx文件。這裏是Views/HelloWorld文件夾中的索引文件。
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Bla bla bla..
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index of HelloWord</h2>
<%:ViewData["Message"]%>
</asp:Content>
這裏是控制器文件夾內的HelloWorldController.cs文件應該是什麼樣的;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace App.Controllers {
public class HelloWorldController : Controller {
public ActionResult Index() {
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
}
}
編輯:下面是這種剃刀版本;
您的母版頁是您的Views/Shared文件夾中的_layout.cshtml文件。這裏是Views/HelloWorld文件夾中的index.cshtml文件。
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@ViewBag.Message
</p>
這裏是什麼_ViewStart.cshtml文件應該看起來像裏面的Views文件夾;
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
這裏是控制器文件夾裏面的HelloWorldController.cs文件應該是這樣的;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace App.Controllers {
public class HelloWorldController : Controller {
public ActionResult Index() {
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
}
}
注意:我這裏假設你的路由其中的Visual Studio開箱當你創建一個新的MVC互聯網應用
這不是在URL \ HelloWorld \ Index上打開母版頁 – Lisa 2011-04-02 22:14:45
@Sheza顯然我沒有在這裏跟蹤你。 \ HelloWorld \ Index視圖與此處的app.controllers.HelloWorld.Index()操作相關。正如你可以看到我的代碼,它有一個佈局引用,我認爲你應該調用,而不是母版頁,如果你正在使用Razor。 – tugberk 2011-04-02 22:25:12
當然,路由引擎不會找到HelloWorld控制器。按照慣例,MVC中的所有控制器都應以「控制器」一詞結束。所以它應該被命名爲HelloWorldController – Hakeem 2011-04-02 23:22:38
母版頁的工作,如模板文件爲您創建的默認路由,最的部分是靜態的,你只需要改變一些地方來顯示慾望的結果。視圖引擎動態生成不同視圖頁面的變化部分。生成動態內容的操作稱爲「渲染」。
使用Razor View Engine時,您需要知道哪些部分是靜態的以及哪些部分正在更改。對於變化的部分,你不會寫任何東西,只是一個神奇的電話@RenderBody()
對於需要在某些視圖中但不在其他部分。您使用@RenderSection("sectionName", false)
。
這裏是一個示例_layout.cshtml文件。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
... your style sheet files ...
</head>
<body>
... shared components...
@RenderBody()
...I like to put the java script files to the end...
@RenderSection("extraScripts", required: false)
</body>
</html>
然後在共享相同的組件每個視圖您在文件的開頭添加
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
。您的視圖文件的其餘部分將由 @RenderBody()
調用獲取。如果你想添加一些自定義腳本爲您的視圖您
@section extraScripts{
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"> </script>
}
額外的腳本添加該微粒頁面會在運行時呈現給你的結果頁面中添加另一段。
你想直接查看母版頁嗎?這對我來說聽起來不合理? – tugberk 2011-04-02 21:54:02
爲什麼不呢?爲什麼不合理? – Lisa 2011-04-02 21:55:06
您正在使用哪個版本的MVC。你的視圖引擎是什麼?剃鬚刀,網頁形式視圖引擎或任何其他? – tugberk 2011-04-02 21:55:08