Asp.net mvc 已定义但尚未在MVC中呈现布局页面

Asp.net mvc 已定义但尚未在MVC中呈现布局页面,asp.net-mvc,Asp.net 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>Crea

我得到这个错误:

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")
}
<!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>
@model BabyStoreII.Models.Category
@{
ViewBag.Title=“创建”;
}
创造
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
类别

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.Name,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Name,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Name,“,new{@class=“text danger”}) } @ActionLink(“返回列表”、“索引”) @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) }
\u Layout.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")
}
<!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>

现代商业-@ViewBag.Title
@ActionLink(“应用程序名称”、“索引”、“主页”、新{area=”“}、新{@class=“navbar品牌”})
  • @ActionLink(“主页”、“索引”、“主页”)
  • @ActionLink(“按类别购物”、“索引”、“类别”)
  • @ActionLink(“查看我们的所有产品”、“索引”、“产品”)
@使用(Html.BeginForm(“Index”,“Products”,FormMethod.Get,new{@class=“navbar form navbar left”})) { @TextBox(“Search”,null,new{@class=“form control”,@placeholder=“Search Products”}) 提交 } @Html.Partial(“_LoginPartial”) @RenderBody()
&抄袭@DateTime.Now.Year-我的ASP.NET应用程序

我的客户端验证不起作用。它只在我将表单发送到服务器时起作用,但当我只是在输入框中失去焦点时,验证不会生效。以下是我的模型验证:

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; }
    }
}
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
名称空间BabyStoreII.Models
{
公共类类别
{
公共int ID{get;set;}
[必需(ErrorMessage=“类别名称不能为空。”)]
[StringLength(50,MinimumLength=3,ErrorMessage=“请输入长度介于3到50个字符之间的类别名称。”)]
[RegularExpression(@“^[A-Z]+[A-zA-Z'-'\s]*$”,ErrorMessage=“请输入以大写字母开头且仅由字母和空格组成的类别。”)]
[显示(名称=“类别名称”)]
公共字符串名称{get;set;}
公共虚拟ICollection产品{get;set;}
}
}

非常感谢您的帮助。

将您的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")
    }
@model BabyStoreII.Models.Category
@{
ViewBag.Title=“创建”;
Layout=Request.IsAjaxRequest()?空:“~/Views/Shared/_Layout.cshtml”;
}
创造
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
类别

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.Name,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Name,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Name,“,new{@class=“text danger”}) } @ActionLink(“返回列表”、“索引”) @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) }
用以下代码替换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")
    }
@model BabyStoreII.Models.Category
@{
ViewBag.Title=“创建”;
Layout=Request.IsAjaxRequest()?空:“~/Views/Shared/_Layout.cshtml”;
}
创造
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
类别

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.Name,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Name,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Name,“,new{@class=“text danger”}) } @ActionLink(“返回列表”、“索引”) @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) }
您需要调用页面上的布局,请更改您的创建.cshtml代码:

@{
Layout=“~/Views/Shared/_Layout.cshtml”;
ViewBag.Title=“创建”;
}

干杯

您需要调用页面上的布局,请更改您的Create.cshtml代码:

@{
Layout=“~/Views/Shared/_Layout.cshtml”;
ViewBag.Title=“创建”;
}

干杯

在您的
布局中
页面缺少
渲染脚本部分
。 所以试试看


create.cshtml
页面中。

layout
页面中,您缺少
render script部分