我想在我的 ASP.NET Core 项目中使用 kendoui。
我有以下部分 -
_Layout.cshtml
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - CarWash</title>
<!-- Bootstrap & Kendo CSS -->
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link href="~/kendoo/css/kendo.rtl.min.css" rel="stylesheet" />
<link href="~/kendoo/css/kendo.common-material.min.css" rel="stylesheet" />
<link href="~/kendoo/css/kendo.default-ocean-blue.min.css" rel="stylesheet" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container-fluid">
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">CarWash</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Service" asp-action="Create">Create Service</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Car" asp-action="Create">Create Car</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Service" asp-action="Index">Index Service</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>
<footer class="border-top footer text-muted">
<div class="container">
© 2024 - CarWash - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div>
</footer>
<!-- Load jQuery, Bootstrap, Kendo, and Site Scripts -->
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/kendoo/js/kendo.all.min.js"></script>
<script src="~/kendoo/js/kendo.aspnetmvc.min.js"></script>
<script src="~/kendoo/js/kendo.web.min.js"></script>
<script src="~/kendoo/js/kendo.messages.fa-ir.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
<!-- Render any additional page scripts -->
@await RenderSectionAsync("Scripts", required: false)
</body>
</html>
我的
Create.cshtml
:
@model CarWash.Models.ServiceModel
<style>
.form-floating > label{
right: 0px !important;
}
</style>
<div class="row" dir="rtl">
<div class="col-md-4 form-floating">
<input id="CustomerName" class="form-control text-end" asp-for="CustomerName" placeholder="Customer Name" />
<label for="CustomerName">Customer Name</label>
</div>
<div class="col-md-4 form-floating">
<input id="PhoneNumber" class="form-control text-end" asp-for="PhoneNumber" placeholder="Phone Number"/>
<label for="PhoneNumber">Phone Number</label>
</div>
@* <select id="CarId" class="form-control" asp-for="CarId" asp-items="(List<SelectListItem>)ViewBag.Cars">
</select> *@
<div class="col-md-4 k-rtl col-md-4">
@(Html.Kendo().ComboBoxFor(x => x.CarId)
.Placeholder("Select Car")
.DataTextField("Text")
.DataValueField("Value")
.DataSource(source =>
{
source.Read(r => r.Action("Car_Read", "Service"));
})
.Filter(FilterType.StartsWith)
)
</div>
</div>
<button class="btn btn-primary mt-3" onclick="Save()">Insert</button>
无法正常工作。我的意思是组合框什么也没显示。但如果我输入以下脚本:
<!-- Load jQuery, Bootstrap, Kendo, and Site Scripts -->
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/kendoo/js/kendo.all.min.js"></script>
<script src="~/kendoo/js/kendo.aspnetmvc.min.js"></script>
<script src="~/kendoo/js/kendo.web.min.js"></script>
<script src="~/kendoo/js/kendo.messages.fa-ir.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
就在下面的部分之前,它起作用了;但我不想那样做:
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>
IO 如何解决这个问题?
根据包含 ASP.NET Core 客户端资源的 Telerik UI 的文档,ASP.NET Core 的 UI 所需的 Kendo UI 脚本文件必须加载到 jQuery 脚本之后的标签中。所以你应该把你的脚本放入
<header>
。
<head>
...
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/themes/8.2.1/bootstrap/bootstrap-main.css" />
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
@* Add the Kendo UI scripts: *@
<script src="https://kendo.cdn.telerik.com/2024.3.806/js/kendo.all.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2024.3.806/js/kendo.aspnetmvc.min.js"></script>
</head>