3
我想加載一個簡單的觀點ASP MVC試圖
@model string
@{
ViewBag.Title = "TestPage";
Layout = "~/Views/Shared/" + Model + ".cshtml";
}
<style>
body {
background-color: black;
}
</style>
<h2>Page_Import</h2>
從弦模型加載佈局,因爲你很可能會看到,我試圖從控制器通過佈局頁面的名稱,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace mvcRockslide03.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult TestImport()
{
return View("_Layout");
}
}
}
,但是當我打開網頁,我得到以下錯誤:
Server Error in '/' Application.
The file "~/Views/Shared/Forum_Layout.cshtml" cannot be requested directly because it calls the "RenderSection" method.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: The file "~/Views/Shared/Forum_Layout.cshtml" cannot be requested directly because it calls the "RenderSection" method.
Source Error:
Line 8: <body>
Line 9: <div class="content-wrapper">
Line 10: @RenderSection("featured", required: false)
Line 11: <section class="content-wrapper main-content clear-fix">
Line 12: @RenderBody()
Source File: f:\mvcRockslide03\mvcRockslide03\Views\Shared\Forum_Layout.cshtml Line: 10
,但是當我改變
@model string
@{
Layout = "~/Views/Shared/" + Model + ".cshtml";
}
到
@{
Layout = "~/Views/Shared/Forum_Layout.cshtml";
}
視圖 和
public ActionResult TestImport()
{
return View("_Layout");
}
到
public ActionResult TestImport()
{
return View();
}
在HomeController中, 它工作正常。
我真的很難過,任何幫助都將得到廣泛的讚賞。
感謝, JMiller
天哪,我覺得愚蠢,這真是棒極了! – jmiller3496 2014-09-18 17:39:20
不,不難過。我認爲這已經讓我們所有的MVC開發者至少陷入了一次。 – Tommy 2014-09-18 18:01:37