Home > Mobile >  Retrieve database value from Shared layout
Retrieve database value from Shared layout

Time:09-18

I added a shared div in _layout that shows the current balance (since i want it to be shown in all pages of the website.)

enter image description here

Right now, The amount is Hard coded, I wrote 3000$ , How can i retrieve this value from the database? There is no controller for the _layout page. I seen in Google the option to use @Html.Action, This is not working ( i only have Html.ActionLink)

Here is the part in _layout:

<div class="container-fluid">
    <div class="row">
        <h1 class="col-6"></h1>
        <h1 class="col-4 text-center" style="font-size:30px;font-weight:bold;color:forestgreen;border-style:inset">
            <img src="~/Images/Coin.jpg" alt="Site Logo" style="height:30px; width: 30px" />
            Current Balance : 3000$
        </h1>
        </div>
        <main role="main" class="pb-3">
            @RenderBody();
        </main>
    </div> 

Created HomeController : enter image description here

CodePudding user response:

In Asp.net Core, to execute the controller action and render the view, you could use the enter image description here

Edit:

About the "Uncaught ReferenceError: $ is not defined at (index)" error, it seems that the JQuery reference not added success, try to add the JQuery reference before the JQuery script:

    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>  
    <script>
        $(function () {
            $.ajax({
                type: "Get",
                url: "/Home/GetMessage",
                success: function (data) {
                    //update the page content.
                    $('#returndata').html(""); //clear the content
                    $('#returndata').html(data); //add the latest data.
                },
                error: function (response) {
                    console.log(response.responseText);
                }
            });
        });
    </script>

You could also use the CDN reference:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

The _layout.csthml resource as below:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - Test</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />

</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">
                <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">Test</a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-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 flex-sm-row-reverse">
                    <ul class="navbar-nav flex-grow-1">
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
                        </li>
                    </ul>
                </div>
                <div id="txt_navbar_output">
                    <h4 class="col-4 text-center" style="font-size:16px;font-weight:bold;color:forestgreen;border-style:inset;">
                        <img src="~/Images/Coin.jpg" alt="Site Logo" style="height:30px; width: 30px" />
                        Current Balance : <span id="returndata">3000$</span>
                    </h4>
                </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">
            &copy; 2020 - Test - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
        </div>
    </footer> 
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>

    @RenderSection("Scripts", required: false)
    <script>
        $(function () {
            $.ajax({
                type: "Get",
                url: "/Home/GetMessage",
                success: function (data) {
                    //update the page content.
                    $('#returndata').html(""); //clear the content
                    $('#returndata').html(data); //add the latest data.
                },
                error: function (response) {
                    console.log(response.responseText);
                }
            });
        });
    </script>

</body>
</html>
  • Related