1
votes

I am working on an MVC project that I was initially using the Entity Framework database first workflow, but found that this approach was causing me to implement more workarounds than if I had used Code first, so I have started again using code first and migrated the data over. The Index, Details and Delete actions work fine, however I am having trouble with Create and Edit, they are throwing the below error at runtime.

When Working with Database First approach I did not have an issue with this, and I am starting to wonder whether or not I have coded my data Model correctly, as before I created my database with MySQL and EF generated the Model for me.

[HttpException (0x80004005): The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_Layout.cshtml": "Scripts".] System.Web.WebPages.WebPageBase.VerifyRenderedBodyOrSections() +836 System.Web.WebPages.WebPageBase.PopContext() +415 System.Web.WebPages.<>c__DisplayClass3.b__2(TextWriter writer) +311 System.Web.WebPages.WebPageBase.Write(HelperResult result) +108 System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action1 body) +89 System.Web.WebPages.WebPageBase.PopContext() +310 System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +375 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +90 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +833 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList1 filters, ActionResult actionResult) +81 System.Web.Mvc.Async.<>c__DisplayClass21.b__1e(IAsyncResult asyncResult) +186 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +38 System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +65 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +36 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38 System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +44 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +65 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +399 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +157

My _Layout.cshtml is as follows

@using System.Web.Optimization
<!DOCTYPE html>
@RenderSection("meta", false)
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>
        @if (!string.IsNullOrWhiteSpace(ViewBag.Title)) {
            @ViewBag.Title@: -
        }
        ResourceBase
    </title>
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <link href="~/Content/css/font-awesome.min.css" rel="stylesheet">
    <link href="~/Content/css/icheck/blue.min.css" rel="stylesheet">
    @Styles.Render("~/Bundles/css")
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body class="skin-red hold-transition sidebar-mini">
    <div class="wrapper">
        @Html.Partial("_Header")
        @Html.Partial("_Sidebar")

        <div class="content-wrapper">
            @*<section class="content-header">
                <h1>
                    @ViewBag.Title
                    <small>@ViewBag.Description</small>
                </h1>
            </section>*@

            <section class="content">
                @RenderBody()
            </section>
        </div>

        @Html.Partial("_ControlSidebar")
    </div>

    <script src="@RouteJs.RouteJsHandler.HandlerUrl"></script>
    @Scripts.Render("~/Bundles/js")
</body>
</html>
1
Can you add your edit view code as well?Juan
Please note that the model-view-controller tag is for questions about the pattern. There is a specific tag for the ASP.NET-MVC implementation.user3559349

1 Answers

1
votes

The error has nothing to do with EF or your models. It is because your Create.cshtml and Edit.cshtml views have included the following

@section scripts {
    ....
}

but your Layout view does not define a placeholder for the a section named scripts. In your layout file (preferably immediately before the closing </body> tag), add

@RenderSection("scripts", required: false)