我收到此错误:
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>© @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 :(得分:1)
您需要在页面上调用布局,请更改 Create.cshtml 代码:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.Title = "Create";
}
干杯!!
答案 1 :(得分:1)
在layout
页面中,您遗漏了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>© @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
页面中。
答案 2 :(得分: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")
}