2017-06-05 38 views
0

我得到這個錯誤:定義,但還沒有被渲染爲佈局頁面MVC

The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_Layout.cshtml": "Scripts". 

用下面的代碼:

Create.cshtml

@model BabyStoreII.Models.Category 

@{ 
    ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 


@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>Category</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

_layout .cshtml

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>Modern Business - @ViewBag.Title</title> 

    <!-- Bootstrap --> 
    <link href="@Url.Content("/css/bootstrap.css")" rel="stylesheet"> 
    <link href="@Url.Content("/css/modern-business.css")" rel="stylesheet"> 
    <!--[if lt IE 9]> 
     <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> 
     <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script> 
    <![endif]--> 
</head> 
<body> 
    <div class="navbar navbar-inverse navbar-fixed-top"> 
     <div class="container"> 
      <div class="navbar-header"> 
       <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> 
        <span class="icon-bar"></span> 
        <span class="icon-bar"></span> 
        <span class="icon-bar"></span> 
       </button> 
       @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" }) 
      </div> 
      <div class="navbar-collapse collapse"> 
       <ul class="nav navbar-nav"> 
        <li>@Html.ActionLink("Home", "Index", "Home")</li> 
        <li>@Html.ActionLink("Shop by Category", "Index", "Categories")</li> 
        <li>@Html.ActionLink("View all our Products", "Index", "Products")</li> 
       </ul> 
       @using (Html.BeginForm("Index", "Products", FormMethod.Get, new { @class = "navbar-form navbar-left" })) 
       { 
        <div class="form-group"> 
         @Html.TextBox("Search", null, new { @class = "form-control", @placeholder = "Search Products" }) 
        </div> 
        <button type="submit" class="btn btn-default">Submit</button> 
       } 
       @Html.Partial("_LoginPartial") 
      </div> 
     </div> 
    </div> 
    <div class="container body-content"> 
     @RenderBody() 
     <hr /> 
     <footer> 
      <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p> 
     </footer> 
    </div> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
    <script src="@Url.Content("/js/bootstrap.js")"></script> 
    <script src="@Url.Content("/js/modern-business.js")"></script> 
</body> 
</html> 

而我的客戶端驗證不起作用。它只有當我的形式發送到服務器,但是當我剛剛失去焦點的輸入框,驗證不會踢的作品下面是我的模型驗證:

Category.cs

using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 

namespace BabyStoreII.Models 
{ 
    public class Category 
    { 
     public int ID { get; set; } 
     [Required(ErrorMessage = "The Category name cannot be blank.")] 
     [StringLength(50, MinimumLength = 3, ErrorMessage = "Please enter a category name between 3 and 50 characters in length.")] 
     [RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$", ErrorMessage = "Please enter a category beginning with a capital letter and made up of letters and spaces only.")] 
     [Display(Name="Category Name")] 
     public string Name { get; set; } 
     public virtual ICollection<Product> Products { get; set; } 
    } 
} 

你非常感謝幫助。

+0

它在我的共享文件夾中。你在問什麼? – Ibanez1408

回答

1

In you layout page you missing render script section。 因此,嘗試

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>Modern Business - @ViewBag.Title</title> 

    <!-- Bootstrap --> 
    <link href="@Url.Content("/css/bootstrap.css")" rel="stylesheet"> 
    <link href="@Url.Content("/css/modern-business.css")" rel="stylesheet"> 
    <!--[if lt IE 9]> 
     <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> 
     <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script> 
    <![endif]--> 
</head> 
<body> 
    <div class="navbar navbar-inverse navbar-fixed-top"> 
     <div class="container"> 
      <div class="navbar-header"> 
       <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> 
        <span class="icon-bar"></span> 
        <span class="icon-bar"></span> 
        <span class="icon-bar"></span> 
       </button> 
       @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" }) 
      </div> 
      <div class="navbar-collapse collapse"> 
       <ul class="nav navbar-nav"> 
        <li>@Html.ActionLink("Home", "Index", "Home")</li> 
        <li>@Html.ActionLink("Shop by Category", "Index", "Categories")</li> 
        <li>@Html.ActionLink("View all our Products", "Index", "Products")</li> 
       </ul> 
       @using (Html.BeginForm("Index", "Products", FormMethod.Get, new { @class = "navbar-form navbar-left" })) 
       { 
        <div class="form-group"> 
         @Html.TextBox("Search", null, new { @class = "form-control", @placeholder = "Search Products" }) 
        </div> 
        <button type="submit" class="btn btn-default">Submit</button> 
       } 
       @Html.Partial("_LoginPartial") 
      </div> 
     </div> 
    </div> 
    <div class="container body-content"> 
     @RenderBody() 
     <hr /> 
     <footer> 
      <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p> 
     </footer> 
    </div> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
    <script src="@Url.Content("/js/bootstrap.js")"></script> 
    <script src="@Url.Content("/js/modern-business.js")"></script> 
    @RenderSection("scripts", false) 
</body> 
</html> 

,如果你不使用_ViewStart.cshtml然後用

@{ 
    Layout = Request.IsAjaxRequest() ? null : "~/Views/Shared/_Layout.cshtml"; 
    ViewBag.Title = "Create"; 
} 

在你create.cshtml頁。

0

替換爲您create.cshtml下面的代碼

@model BabyStoreII.Models.Category 

    @{ 
     ViewBag.Title = "Create"; 
Layout = Request.IsAjaxRequest() ? null : "~/Views/Shared/_Layout.cshtml"; 
    } 

    <h2>Create</h2> 


    @using (Html.BeginForm()) 
    { 
     @Html.AntiForgeryToken() 

     <div class="form-horizontal"> 
      <h4>Category</h4> 
      <hr /> 
      @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
      <div class="form-group"> 
       @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) 
       <div class="col-md-10"> 
        @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) 
        @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) 
       </div> 
      </div> 

      <div class="form-group"> 
       <div class="col-md-offset-2 col-md-10"> 
        <input type="submit" value="Create" class="btn btn-default" /> 
       </div> 
      </div> 
     </div> 
    } 

    <div> 
     @Html.ActionLink("Back to List", "Index") 
    </div> 

    @section Scripts { 
     @Scripts.Render("~/bundles/jqueryval") 
    } 
1

你需要調用佈局頁面上,請更改您Create.cshtml代碼:

@{ 
    Layout = "~/Views/Shared/_Layout.cshtml";  
    ViewBag.Title = "Create"; 
} 

乾杯!

相關問題