Теги скрипта не смогли загрузить статус ресурса 404 - PullRequest
0 голосов
/ 16 марта 2019

У меня есть проект ASP.NET MVC, и на одной из страниц у меня есть следующий код со скриптом внизу ...

@model IEnumerable<PixelBox.Dtos.ItemGetDto>

@{
    ViewBag.Title = "Index";
}
<body>
    <h2>Index</h2>

    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    @foreach (var item in Model)
    {
        var dateString = item.DateListed.ToShortDateString();
        var imagePath = item.ImagePath.ToString() + ".png";



        <div class="card" style="width: 18rem; display:inline-block;">
            <img class="card-img-top" src="~/ItemImages/@imagePath" + alt="Card image cap">
            <div class="card-body">
                <h5 class="card-title"> @Html.DisplayFor(modelItem => item.Name) </h5>
                <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            </div>
            <ul class="list-group list-group-flush">
                <li class="list-group-item">Price:   @Html.DisplayFor(modelItem => item.Price)</li>
                <li class="list-group-item">Date Listed:  @dateString</li>
            </ul>
            <div class="card-body">
                <input type="button" value="Use Shipping Address" id="AddToBasket" />
            </div>
        </div>
    }
</body>
<script type="text/javascript" >
    $(document).ready(function () {

        function AddToBasket(id) {
            debugger;
        }

    })
</script>

Однако, когда это отрисовывается, на инструментах DEV говорится, что ему не удалось загрузить ресурс enter image description here

На моей странице макета есть следующий код ...

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)

</head>

А также мой конфиг пакета ...

 public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.validate*"));

            // Use the development version of Modernizr to develop with and learn from. Then, when you're
            // ready for production, use the build tool at https://modernizr.com to pick only the tests you need.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                      "~/Scripts/bootstrap.js"));

            bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/bootstrap.css",
                      "~/Content/site.css"));
        }

Я проверил путь к файлам, и он правильный

1 Ответ

0 голосов
/ 17 марта 2019

Вы должны добавить макет для своей страницы

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

И так как у вас есть скрипты разделов в макете, вы должны переместить свой скрипт внутри него.

@section scripts {
 <script>
    $(document).ready(function () {
        alert('ok?')
        function AddToBasket(id) {
            debugger;
        }
    })
</script>
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...