我正在學習ASP.NET MVC,並試圖讓我的頭部部分視圖。我正在嘗試一個非常簡單的事情,這裏是我的源代碼。部分視圖覆蓋主視圖
_Layout.cshtml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
@RenderBody()
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
</body>
</html>
_ViewStart.cshtml
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication4.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public PartialViewResult UpdateDate()
{
return PartialView("Partial1");
}
}
}
Index.cshtml
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@DateTime.Now
<br />
@{Html.RenderPartial("Partial1");}
<br />
@Html.ActionLink("Render Partial", "UpdateDate")
Partial1.cshtml
@DateTime.Now
現在,當我點擊渲染部分鏈接UpdateDate動作方法被調用,但渲染局部視圖覆蓋的主要內容,我只看到局部視圖的內容。爲什麼我會丟失Index.cshtml的內容?
我需要做什麼才能顯示Index.cshtml的內容,並且僅顯示部分視圖內容?
感謝您的解釋。 – user2265934 2013-04-10 12:49:51