• Web开发:ASP.NET CORE的前端demo(纯前端)


    目录

    一、建立项目

    二、删除无用文件

    三、样式添加

    四、写一个登录页面

     五、登录主界面

    一、建立项目

    二、删除无用文件

    三、样式添加

    将你的图片资源添加在wwwroot下方,例如pics/logo.png

    四、写一个登录页面

    将Privacy.cshtml改为 Forget.cshtml ,并且在HomeController中的方法也改一下:

    1. public IActionResult Forget() //点击密码时返回视图
    2. {
    3. return View();
    4. }

    并且在 Forget.cshtml 写下如下代码:

    <h1>忘记密码页面h1>

    然后在Index.cshtml中写下如下代码

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>登录页面title>
    7. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    8. <style>
    9. body {
    10. background-color: #f8f9fa;
    11. font-size: 20px;
    12. }
    13. .login-container {
    14. display: flex;
    15. justify-content: center;
    16. align-items: center;
    17. height: 100vh;
    18. }
    19. .login-form {
    20. max-width: 400px;
    21. padding: 35px;
    22. background-color: #fff;
    23. border-radius: 10px;
    24. box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.1);
    25. }
    26. .login-form h2 {
    27. text-align: center;
    28. margin-bottom: 40px;
    29. }
    30. .form-group label {
    31. font-size: 22px;
    32. }
    33. .form-control {
    34. height: 40px;
    35. font-size: 18px;
    36. margin-bottom: 20px; /* 增加文本框的下间距 */
    37. }
    38. .form-actions {
    39. display: flex; /* 设置为 Flex 容器 */
    40. justify-content: flex-end; /* 将子元素对齐到容器的末尾 */
    41. align-items: center; /* 垂直居中子元素 */
    42. margin-top: 1rem; /* 添加一些上边距以与表单字段分隔 */
    43. }
    44. .btn {
    45. /* 确保按钮和链接看起来相似,根据需要调整样式 */
    46. padding: 0.5rem 1rem;
    47. color: white;
    48. background-color: #007bff;
    49. border: none;
    50. border-radius: 0.25rem;
    51. cursor: pointer;
    52. margin:20px;
    53. }
    54. .btn:hover {
    55. background-color: #0056b3; /* 悬停效果 */
    56. }
    57. .logo {
    58. width: 120px;
    59. height: auto;
    60. display: block;
    61. margin: 0 auto;
    62. margin-bottom: 30px;
    63. }
    64. style>
    65. head>
    66. <body>
    67. <div class="login-container">
    68. <div class="login-form">
    69. <img src="/pics/logo.png" alt="Logo" class="logo">
    70. <h2>XXX管理系统h2>
    71. <form method="post" action="/Home/Index">
    72. <div class="form-group">
    73. <label for="username">账号: label>
    74. <input type="text" class="form-control" id="username" placeholder="请输入您的帐号:" Name="id" >
    75. div>
    76. <div class="form-group">
    77. <label for="password">密码: label>
    78. <input type="password" class="form-control" id="password" placeholder="请输入您的密码:" Name="pwd">
    79. div>
    80. <div class="form-actions">
    81. <button type="submit" class="btn">登录button>
    82. <button type="submit" class="btn" onclick="window.open('/Home/Forget')">忘记密码button>
    83. div>
    84. form>
    85. div>
    86. div>
    87. <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js">script>
    88. <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js">script>
    89. body>
    90. html>

    再创建一个控制器和界面(MainController 和 Main/Index): 

    (伪)后端 HomeController:

    1. public static bool Loginflag = false;
    2. [HttpPost]
    3. public IActionResult Index(string id,string pwd)
    4. {
    5. if (pwd!=null && pwd.Equals("123"))
    6. {
    7. Loginflag = true;
    8. return RedirectToAction("Index", "Main");//重定向到MainController下的Index界面
    9. }
    10. else
    11. {
    12. return View();
    13. }
    14. }

    (伪)后端 MainController:

    1. public IActionResult Index()
    2. {
    3. if(HomeController.Loginflag)//如果登录成功
    4. {
    5. return View();//打开当前界面
    6. }
    7. else
    8. {
    9. return RedirectToAction("Index","Home");//重定向到HomeController下的Index界面
    10. }
    11. }

    效果:密码输入为123时,登录成功;点击忘记密码会跳转到忘记密码页面。

     【更好的传入方式】:封装成一个类传入

     五、登录主界面

     (伪)后端 MainController:

    1. using Microsoft.AspNetCore.Mvc;
    2. using Microsoft.AspNetCore.Mvc.RazorPages;
    3. using WebApplication1.Models;
    4. namespace WebApplication1.Controllers
    5. {
    6. public class MainController : Controller
    7. {
    8. public IActionResult Index(int? page, QueryParameters? parameters=null)//QueryParameters类需要自主创建
    9. {
    10. if(HomeController.Loginflag)//如果登录成功
    11. {
    12. //默认用户名
    13. ViewBag.UserName = "小明";
    14. // 获取输入框的查询数据
    15. string name = parameters?.Name;
    16. string className = parameters?.ClassName;
    17. int? age = parameters?.Age;
    18. string gender = parameters?.Gender;
    19. bool? under18 = parameters?.Under18;
    20. #region 模拟数据库查询
    21. var list = new List();
    22. list.Add(new Student { Id=1,Name="小虹",age=18,sex="女",classes="计算机1班"});
    23. list.Add(new Student { Id = 2, Name = "小明", age = 19, sex = "男", classes = "计算机2班" });
    24. list.Add(new Student { Id = 3, Name = "小华", age = 17, sex = "女", classes = "计算机3班" });
    25. list.Add(new Student { Id = 4, Name = "小张", age = 20, sex = "男", classes = "数学1班" });
    26. list.Add(new Student { Id = 5, Name = "小李", age = 18, sex = "女", classes = "物理2班" });
    27. list.Add(new Student { Id = 6, Name = "小王", age = 19, sex = "男", classes = "化学3班" });
    28. list.Add(new Student { Id = 7, Name = "小赵", age = 21, sex = "女", classes = "生物1班" });
    29. list.Add(new Student { Id = 8, Name = "小陈", age = 17, sex = "男", classes = "英语2班" });
    30. list.Add(new Student { Id = 9, Name = "小刘", age = 18, sex = "女", classes = "历史3班" });
    31. list.Add(new Student { Id = 10, Name = "小周", age = 19, sex = "男", classes = "地理1班" });
    32. list.Add(new Student { Id = 11, Name = "小吴", age = 20, sex = "女", classes = "政治2班" });
    33. list.Add(new Student { Id = 12, Name = "小郑", age = 17, sex = "男", classes = "语文3班" });
    34. list.Add(new Student { Id = 13, Name = "小孙", age = 18, sex = "女", classes = "美术1班" });
    35. list.Add(new Student { Id = 14, Name = "小袁", age = 19, sex = "男", classes = "音乐2班" });
    36. list.Add(new Student { Id = 15, Name = "小许", age = 20, sex = "女", classes = "体育3班" });
    37. list.Add(new Student { Id = 16, Name = "小徐", age = 21, sex = "男", classes = "信息1班" });
    38. #endregion
    39. int pageSize = 10; // 每页显示的数据量
    40. int pageNumber = (page ?? 1); // 当前页数,默认为第一页
    41. ViewBag.CurrentPage = pageNumber;
    42. ViewBag.TotalPages = (int)Math.Ceiling((double)list.Count / pageSize); // 计算总页数
    43. ViewBag.ResultList = list.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList(); ;
    44. return View();//打开当前界面
    45. }
    46. else
    47. {
    48. return RedirectToAction("Index","Home");//重定向到HomeController下的Index界面
    49. }
    50. }
    51. ///
    52. /// 点击修改按钮
    53. ///
    54. ///
    55. ///
    56. [HttpPost]
    57. public IActionResult Index([FromBody] RetunData data)//RetunData类需要自主创建
    58. {
    59. int id = data.Id;
    60. return Ok(); // 返回成功响应
    61. }
    62. }
    63. }

    Main/Index界面:

    1. html>
    2. <html>
    3. <head>
    4. <title>导航栏示例title>
    5. <style>
    6. body {
    7. margin: 0;
    8. padding: 0;
    9. }
    10. .navbar {
    11. background-color: #007bff;
    12. color: white;
    13. padding: 10px;
    14. margin: 0;
    15. width: 100%;
    16. box-sizing: border-box;
    17. }
    18. .navbar-items {
    19. list-style-type: none;
    20. padding: 0;
    21. margin: 0;
    22. width:200px;
    23. background-color: ghostwhite; /* 您可以根据需要更改背景颜色 */
    24. }
    25. .navbar-items li {
    26. display: block; /* 设置为块级元素,使它们竖着排列 */
    27. padding: 10px;
    28. }
    29. .navbar-items li:hover {
    30. background-color: #ddd; /* 鼠标悬停时的背景颜色 */
    31. cursor: pointer; /* 鼠标悬停时变为手形图标 */
    32. }
    33. th, td {
    34. border: 1px solid #dddddd;
    35. text-align: left;
    36. padding: 8px;
    37. }
    38. tr:nth-child(even) {
    39. background-color: #f2f2f2;
    40. }
    41. style>
    42. head>
    43. <body>
    44. <div class="navbar">欢迎您!@ViewBag.UserNamediv>
    45. <ul class="navbar-items" style="text-align: center;">
    46. <li><a href="#" style="color: inherit; text-decoration: none;">项目1a>li>
    47. <li><a href="#" style="color: inherit; text-decoration: none;">项目2a>li>
    48. <li><a href="#" style="color: inherit; text-decoration: none;">项目3a>li>
    49. <li><a href="#" style="color: inherit; text-decoration: none;">项目4a>li>
    50. <li><a href="#" style="color: inherit; text-decoration: none;">项目5a>li>
    51. ul>
    52. <div style="text-align: center; position: absolute; left:350px; top:70px;">
    53. <input type="text" id="nameInput" placeholder="姓名" style="margin:10px">
    54. <input type="text" id="classInput" placeholder="班级" style="margin:10px">
    55. <input type="text" id="ageInput" placeholder="年龄" style="margin:10px">
    56. <select id="genderSelect" style="margin:10px">
    57. <option value="男">option>
    58. <option value="女">option>
    59. select>
    60. <input type="checkbox" id="under18Checkbox" style="margin:10px"> 小于18岁
    61. <button id="refreshButton" type="button" style="background-color: greenyellow; border: none; padding: 5px 10px; cursor: pointer;">刷新查询button>
    62. <button type="button" style="background-color: cornflowerblue; border: none; padding: 5px 10px; cursor: pointer;">新增信息button>
    63. div>
    64. <table style="margin: 20px auto;border-collapse: collapse; position: absolute;left:550px; top:130px;">
    65. <thead>
    66. <tr style="margin: 20px auto;">
    67. <th>姓名th>
    68. <th>班级th>
    69. <th>年龄th>
    70. <th>性别th>
    71. <th>操作th>
    72. tr>
    73. thead>
    74. <tbody>
    75. @foreach (var item in ViewBag.ResultList)
    76. {
    77. <tr>
    78. <td>@item.Nametd>
    79. <td>@item.classestd>
    80. <td>@item.agetd>
    81. <td>@item.sextd>
    82. <td>
    83. <button type="button" style="background-color: orange; border: none; padding: 5px 10px; cursor: pointer;" onclick="modifyRecord('@item.Id')">修改button>
    84. <button type="button" style="background-color: #dc143c; border: none; padding: 5px 10px; cursor: pointer;">删除button>
    85. td>
    86. tr>
    87. }
    88. tbody>
    89. table>
    90. <div class="pagination" style="text-align: center; position: absolute;left:600px; top:650px; margin:5px">
    91. <button type="button" onclick="location.href='@Url.Action("Index", new { page = ViewBag.CurrentPage - 1 })'" @(ViewBag.CurrentPage == 1 ? "disabled" : "")>上一页button>
    92. <span>第 @ViewBag.CurrentPage 页/共 @ViewBag.TotalPages 页 span>
    93. <button type="button" onclick="location.href='@Url.Action("Index", new { page = ViewBag.CurrentPage + 1 })'" @(ViewBag.CurrentPage == ViewBag.TotalPages ? "disabled" : "")>下一页button>
    94. div>
    95. <script src="https://code.jquery.com/jquery-3.6.0.min.js">script>
    96. <script>
    97. // 监听按钮点击事件
    98. $('#refreshButton').on('click', function () {
    99. // 获取输入框和选择框的值
    100. var name = $('#nameInput').val();
    101. var className = $('#classInput').val();
    102. var age = $('#ageInput').val();
    103. var gender = $('#genderSelect').val();
    104. var under18 = $('#under18Checkbox').is(':checked');
    105. // 发送Ajax请求
    106. $.ajax({
    107. url: '/Main/Index', // 后端处理方法的URL
    108. method: 'GET', // 使用GET方法发送请求
    109. data: {
    110. name: name,
    111. className: className,
    112. age: age,
    113. gender: gender,
    114. under18: under18
    115. },
    116. success: function (response) {
    117. // 请求成功后的处理逻辑
    118. console.log('后端处理成功');
    119. },
    120. error: function (xhr, status, error) {
    121. // 请求失败后的处理逻辑
    122. console.error('后端处理失败:', error);
    123. }
    124. });
    125. });
    126. function modifyRecord(id) {
    127. // 创建一个包含ID的对象(POST请求的特点)
    128. var data = { id: id };
    129. // 发送POST请求
    130. $.ajax({
    131. url: '/Main/Index', // 根据你的实际路由进行修改
    132. type: 'POST',
    133. contentType: 'application/json',
    134. data: JSON.stringify(data),
    135. success: function (response) {
    136. // 处理成功响应
    137. console.log('Record modified successfully');
    138. },
    139. error: function (xhr, status, error) {
    140. // 处理错误响应
    141. console.error('Error modifying record:', error);
    142. }
    143. });
    144. }
    145. script>
    146. body>
    147. html>

    【效果如下所示】:

    当然这只是个demo,很多功能都没有实现,只是写一下前端以及前后端是如何交互的。

  • 相关阅读:
    R语言使用rbind函数将多个dataframe数据纵向合并起来创建dataframe数据、按照行合并dataframe
    MemoryCache 缓存 实用
    nacos的服务发现详解
    Webpack5学习大纲笔记
    ROS机器人移动到指定坐标点
    【线性代数】二次型总结
    阿里巴巴最新总结「百亿级别并发设计手册」GitHub收获70K标星
    【接口测试】如何在 Eolink Apilkit 中使用 cookie ?
    perf性能分析
    【Java Web】论坛帖子添加评论
  • 原文地址:https://blog.csdn.net/m0_67412019/article/details/137840978