Я пытаюсь загрузить простое представление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
Golly, я чувствую себя глупо, что работал отлично! – jmiller3496
Нет, не чувствую себя плохо. Я думаю, что укусил всех нас разработчиками MVC хотя бы один раз. – Tommy