尝试在我的某个观看次数

时间:2017-05-15 11:35:00

标签: c# asp.net-mvc entity-framework

我正在开发一个MVC项目,我最初使用Entity Framework数据库的第一个工作流,但发现这种方法导致我实现了比我先使用Code更多的变通方法,所以我再次使用代码开始并将数据迁移过来。索引,详细信息和删除操作工作正常,但我在创建和编辑时遇到问题,他们在运行时抛出以下错误。

使用Database First方法时我没有遇到这个问题,我开始怀疑我是否正确编码了我的数据模型,就像之前我使用MySQL创建数据库而EF为我生成了模型。

  

[HttpException(0x80004005):已定义以下部分,但尚未针对布局页面“〜/ 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,Action 1 body) +89 System.Web.WebPages.WebPageBase.PopContext() +310 System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +375 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList 1个过滤器,Int32 filterIndex,ResultExecutingContext preContext,ControllerContext controllerContext,ActionResult actionResult)+90      System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList 1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +833 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList 1个过滤器,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.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +65 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53 System.Web.Mvc.Async.WrappedAsyncVoid 1.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

我的_Layout.cshtml如下

@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 个答案:

答案 0 :(得分:1)

错误与EF或您的模型无关。这是因为您的Create.cshtmlEdit.cshtml观看次数包含以下内容

@section scripts {
    ....
}

但您的Layout视图未定义名为scripts的部分的占位符。在您的布局文件中(最好在结束</body>标记之前),添加

@RenderSection("scripts", required: false)