Home > OS >  Add form to layout
Add form to layout

Time:12-08

I need to add a login popup to the header of every page, so naturally I want to add it to the layout as a partial view.

The problem is, the layout doesnt have a pagemodel.

We do use a BasePageModel that every page inherits from, where I can add 2 strings for username/password. But how would the layout see those fields?

CodePudding user response:

You can specify a model for the Layout page just as you would a standard content page:

@model BasePageModel
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    ...

Then your properties are accessible via the Model property of the Layout page. The BasePageModel will also be passed to any partials that you add to the layout (unless you specify a different model for the partial), so you can also access the properties in those.

CodePudding user response:

I need to add a login popup to the header of every page, so naturally I want to add it to the layout as a partial view.

According to your description, I do a demo for that situation. But I don’t use a BasePageModel that every page inherits from.

The demo as below, hoping it can help you.

1.Add a Login page with page model, and post method

Login.cshtml.cs:

 public class LoginModel : PageModel
    {
      
        [BindProperty]
        public string Username { get; set; }

        [BindProperty]
        public string Password { get; set; }

        public string Msg { get; set; }

        public void OnGet()
        {
        }

        public IActionResult OnPost(string Username, string Password)
        {
           //do your other things...
            return Page();
        }
    }

Login.cshtml:

@page
@model Login.Pages.LoginModel
@{
}
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Login</title>
</head>
<body>

    <h3>Login Form</h3>
    @Model.Msg
    <form method="post" asp-page="Login">
        <table>
            <tr>
                <td>Username</td>
                <td><input type="text" asp-for="@Model.Username" /></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" asp-for="@Model.Password" /></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><input type="submit" value="Login" /></td>
            </tr>
        </table>
    </form>
</body>
</html>
  1. Add the login form in the layout. Using name attribute: change input type="text" asp-for="@Model.Username" into input type="text" name="Username"
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <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">
<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-page="/Index">Home</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>
                        </li>

                    </ul>
                   </div>
                        <div>

                            <fieldset>

                                <div class="container">
                                    <div class="row">
                                        <div class="col-xs-12">

                                            <button id="btnShowModal" type="button"
                                                    class="btn btn-sm btn-default pull-left col-lg-11 button button4">
                                                login
                                            </button>

                                            <div class="modal fade" tabindex="-1" id="loginModal"
                                                 data-keyboard="false" data-backdrop="static">
                                                <div class="modal-dialog modal-lg">
                                                    <div class="modal-content">
                                                        <div class="modal-header">
                                                            <button type="button" class="close" data-dismiss="modal">
                                                                ×
                                                            </button>

                                                        </div>
                                                        <div class="modal-body">
                                                            <form method="post" asp-page="Login">
                                                                <table border="0" cellpadding="2" cellspacing="2">
                                                                    <tr>
                                                                        <td>Username</td>
                                                                        <td><input type="text" name="Username"></td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td>Password</td>
                                                                        <td><input type="password" name="Password"></td>
                                                                    </tr>
                                                                    <tr>
                                                                        <td>&nbsp;</td>
                                                                        <td><input type="submit" value="Login"></td>
                                                                    </tr>
                                                                </table>
                                                            </form>

                                                        </div>

                                                    </div>
                                                </div>
                                            </div>

                                        </div>
                                    </div>
                                </div>
                                
                            </fieldset>
                        </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; 2021 - Login - <a asp-area="" asp-page="/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>

    @await RenderSectionAsync("Scripts", required: false)
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnShowModal").click(function () {
                $("#loginModal").modal('show');
            });

            $("#btnHideModal").click(function () {
                $("#loginModal").modal('hide');
            });
        });
    </script>
</body>
</html>


Results:

enter image description here

enter image description here

  • Related