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

目录

一、建立项目

二、删除无用文件

三、样式添加

四、写一个登录页面

 五、登录主界面

一、建立项目

二、删除无用文件

三、样式添加

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

四、写一个登录页面

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

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

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

<h1>忘记密码页面</h1>

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

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>登录页面</title><!-- Bootstrap CSS --><link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"><!-- Custom styles --><style>body {background-color: #f8f9fa;font-size: 20px;}.login-container {display: flex;justify-content: center;align-items: center;height: 100vh;}.login-form {max-width: 400px;padding: 35px;background-color: #fff;border-radius: 10px;box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.1);}.login-form h2 {text-align: center;margin-bottom: 40px;}.form-group label {font-size: 22px;}.form-control {height: 40px;font-size: 18px;margin-bottom: 20px; /* 增加文本框的下间距 */}.form-actions {display: flex; /* 设置为 Flex 容器 */justify-content: flex-end; /* 将子元素对齐到容器的末尾 */align-items: center; /* 垂直居中子元素 */margin-top: 1rem; /* 添加一些上边距以与表单字段分隔 */}.btn {/* 确保按钮和链接看起来相似,根据需要调整样式 */padding: 0.5rem 1rem;color: white;background-color: #007bff;border: none;border-radius: 0.25rem;cursor: pointer;margin:20px;}.btn:hover {background-color: #0056b3; /* 悬停效果 */}.logo {width: 120px;height: auto;display: block;margin: 0 auto;margin-bottom: 30px;}</style>
</head>
<body><div class="login-container"><div class="login-form"><img src="/pics/logo.png" alt="Logo" class="logo"><h2>XXX管理系统</h2><form method="post" action="/Home/Index"><div class="form-group"><label for="username">账号: </label><input type="text" class="form-control" id="username" placeholder="请输入您的帐号:" Name="id" ></div><div class="form-group"><label for="password">密码: </label><input type="password" class="form-control" id="password" placeholder="请输入您的密码:" Name="pwd"></div><div class="form-actions"><!-- 创建一个新的 div 作为 Flex 容器 --><button type="submit" class="btn">登录</button><button type="submit" class="btn" onclick="window.open('/Home/Forget')">忘记密码</button></div></form></div></div><!-- Bootstrap JS --><script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script><script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script></body>
</html>

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

(伪)后端 HomeController:

public static bool Loginflag = false;[HttpPost]
public IActionResult Index(string id,string pwd)
{if (pwd!=null && pwd.Equals("123")){Loginflag = true;return RedirectToAction("Index", "Main");//重定向到MainController下的Index界面}else{return View();}    
}

(伪)后端 MainController:

public IActionResult Index()
{if(HomeController.Loginflag)//如果登录成功{return View();//打开当前界面}else{return RedirectToAction("Index","Home");//重定向到HomeController下的Index界面}
}

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

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

 五、登录主界面

 (伪)后端 MainController:

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

Main/Index界面:

<!DOCTYPE html>
<html>
<head><title>导航栏示例</title><style>body {margin: 0;padding: 0;}.navbar {background-color: #007bff;color: white;padding: 10px;margin: 0;width: 100%;box-sizing: border-box;}.navbar-items {list-style-type: none;padding: 0;margin: 0;width:200px;background-color: ghostwhite; /* 您可以根据需要更改背景颜色 */}.navbar-items li {display: block; /* 设置为块级元素,使它们竖着排列 */padding: 10px;}.navbar-items li:hover {background-color: #ddd; /* 鼠标悬停时的背景颜色 */cursor: pointer; /* 鼠标悬停时变为手形图标 */}th, td {border: 1px solid #dddddd;text-align: left;padding: 8px;}tr:nth-child(even) {background-color: #f2f2f2;}</style>
</head>
<body><div class="navbar">欢迎您!@ViewBag.UserName</div><ul class="navbar-items" style="text-align: center;"><li><a href="#" style="color: inherit; text-decoration: none;">项目1</a></li><li><a href="#" style="color: inherit; text-decoration: none;">项目2</a></li><li><a href="#" style="color: inherit; text-decoration: none;">项目3</a></li><li><a href="#" style="color: inherit; text-decoration: none;">项目4</a></li><li><a href="#" style="color: inherit; text-decoration: none;">项目5</a></li></ul><div style="text-align: center; position: absolute; left:350px; top:70px;"><input type="text" id="nameInput" placeholder="姓名" style="margin:10px"><input type="text" id="classInput" placeholder="班级" style="margin:10px"><input type="text" id="ageInput" placeholder="年龄" style="margin:10px"><select id="genderSelect" style="margin:10px"><option value="男">男</option><option value="女">女</option></select><input type="checkbox" id="under18Checkbox" style="margin:10px"> 小于18岁<button id="refreshButton" type="button" style="background-color: greenyellow; border: none; padding: 5px 10px; cursor: pointer;">刷新查询</button><button type="button" style="background-color: cornflowerblue; border: none; padding: 5px 10px; cursor: pointer;">新增信息</button></div><!-- 列表 --><table style="margin: 20px auto;border-collapse: collapse; position: absolute;left:550px; top:130px;"><thead><tr style="margin: 20px auto;"><th>姓名</th><th>班级</th><th>年龄</th><th>性别</th><th>操作</th></tr></thead><tbody>@foreach (var item in ViewBag.ResultList){<tr><td>@item.Name</td><td>@item.classes</td><td>@item.age</td><td>@item.sex</td><!-- 继续添加更多字段 --><td><button type="button" style="background-color: orange; border: none; padding: 5px 10px; cursor: pointer;" onclick="modifyRecord('@item.Id')">修改</button><button type="button" style="background-color: #dc143c; border: none; padding: 5px 10px; cursor: pointer;">删除</button></td></tr>}</tbody></table><!-- 上一页/下一页按钮以及页码 --><div class="pagination" style="text-align: center;  position: absolute;left:600px; top:650px; margin:5px"><button type="button" onclick="location.href='@Url.Action("Index", new { page = ViewBag.CurrentPage - 1 })'" @(ViewBag.CurrentPage == 1 ? "disabled" : "")>上一页</button><span>第 @ViewBag.CurrentPage 页/共 @ViewBag.TotalPages 页 </span><button type="button" onclick="location.href='@Url.Action("Index", new { page = ViewBag.CurrentPage + 1 })'" @(ViewBag.CurrentPage == ViewBag.TotalPages ? "disabled" : "")>下一页</button></div><script src="https://code.jquery.com/jquery-3.6.0.min.js"></script><script>// 监听按钮点击事件$('#refreshButton').on('click', function () {// 获取输入框和选择框的值var name = $('#nameInput').val();var className = $('#classInput').val();var age = $('#ageInput').val();var gender = $('#genderSelect').val();var under18 = $('#under18Checkbox').is(':checked');// 发送Ajax请求$.ajax({url: '/Main/Index', // 后端处理方法的URLmethod: 'GET', // 使用GET方法发送请求data: {name: name,className: className,age: age,gender: gender,under18: under18},success: function (response) {// 请求成功后的处理逻辑console.log('后端处理成功');},error: function (xhr, status, error) {// 请求失败后的处理逻辑console.error('后端处理失败:', error);}});});function modifyRecord(id) {// 创建一个包含ID的对象(POST请求的特点)var data = { id: id };// 发送POST请求$.ajax({url: '/Main/Index',  // 根据你的实际路由进行修改type: 'POST',contentType: 'application/json',data: JSON.stringify(data),success: function (response) {// 处理成功响应console.log('Record modified successfully');},error: function (xhr, status, error) {// 处理错误响应console.error('Error modifying record:', error);}});}</script>
</body>
</html>

【效果如下所示】:

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/826024.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【数学】深度学习中的概率基础知识记录

基于 Deep Learning (2017, MIT) 书总结了必要的概率知识 原blog 以及用到的Ipython notebook 文章目录 1 概述2 知识2.1 离散变量和概率质量函数&#xff08;PMF&#xff09;2.2 连续变量和概率密度函数&#xff08;PDF&#xff09;2.3 边缘概率2.4 条件概率2.5 条件概率的链式…

阿里云ECS服务器安装docker

首先查看阿里云ECS的服务器的版本 cat /etc/redhat-release如果是Alibaba Cloud Linux release 3,请执行以下命令 添加docker-ce的dnf源。 sudo dnf config-manager --add-repohttps://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo安装Alibaba Cloud Linux 3专…

MongoDB 索引全攻略

目录 一、索引介绍 1.1 单字段索引 1.2 复合索引 1.3 多键索引 1.4 主键索引 1.5 TTL 索引 1.6 地理空间索引 1.7 哈希索引 1.8 创建索引时注意事项 1.9 索引效果查看 二、索引实现原理 2.1 为什么使用 B-Tree 三、执行计划 一、索引介绍 任何数据库都有索引这一核心功能&…

Yolov8项目实践——基于yolov8与OpenCV实现目标物体运动热力图

概述 在数据驱动和定位的世界中&#xff0c;对数据进行解释、可视化和决策的能力变得日益重要。这表明&#xff0c;使用正确的工具和技术可能是项目成功的关键。在计算机视觉领域&#xff0c;存在许多技术来解释从视频&#xff08;包括录像、流媒体或实时视频&#xff09;中获…

SpringMVC核心流程解析

SpringMVC核心流程解析 DispatcherServlet的继承关系请求流程分析获取HandlerChain(ControllrtMethod拦截器)获取HandlerAdapter handlerMappings的初始化过程 DispatcherServlet的继承关系 DispatcherServlet本质是一个servlet&#xff0c;既然是servlet&#xff0c;一个请求…

[Algorithm][滑动窗口][水果成篮][最大连续的一个数 Ⅲ][将x减到0的最小操作数]详细讲解

目录 1.水果成篮1.题目链接2.算法原理讲解3.代码讲解 2.找到字符串中所有字母异位词1.题目链接2.算法原理讲解3.代码实现 3.串联所有单词的字串1.题目链接2.算法原理讲解3.代码实现 3.最小覆盖字串1.题目链接2.算法原理讲解 1.水果成篮 1.题目链接 水果成篮 2.算法原理讲解 …

Java集合进阶——数据结构

1.栈 模型&#xff1a; 栈模型和一个杯子差不多&#xff0c;一端开口&#xff0c;一端封闭&#xff0c;开口的那端叫栈顶&#xff0c;封闭的那端叫栈底&#xff0c;如图所示 介绍&#xff1a; 元素进入栈中叫进栈/压栈&#xff0c;元素出来叫出栈&#xff0c;元素进栈后会先来…

Appian发布最新版本:通过AI流程自动化推动业务发展

Appian公司于2024年4月16日在弗吉尼亚州麦克莱恩宣布推出Appian平台的最新版本。此版本引入了Process HQ&#xff0c;这是一个集流程挖掘和企业AI于一体的系统&#xff0c;结合了Appian的数据平台。Process HQ为企业运营提供前所未有的可见性&#xff0c;支持数据驱动的决策和流…

CERLAB无人机自主框架: 2-动态目标检测与跟踪

前言&#xff1a;更多更新文章详见我的个人博客主页【MGodmonkeyの世界】 描述&#xff1a;欢迎来到CERLAB无人机自主框架&#xff0c;这是一个用于自主无人飞行器 (UAV) 的多功能模块化框架。该框架包括不同的组件 (模拟器&#xff0c;感知&#xff0c;映射&#xff0c;规划和…

Hadoop——Yarn 调度器和调度算法

Yarn 调度器和调度算法 YARN调度器&#xff08;Scheduler&#xff09;是负责将集群资源分配给不同应用程序的组件。它根据应用程序的资源需求和优先级&#xff0c;以及集群的资源供给情况&#xff0c;决定如何分配资源。YARN提供了多种调度器实现&#xff0c;每种调度器都有不…

如何通过MSTSC连接Ubuntu的远程桌面?

正文共&#xff1a;666 字 12 图&#xff0c;预估阅读时间&#xff1a;1 分钟 前面我们介绍了如何通过VNC连接Ubuntu 18.04的远程桌面&#xff08;Ubuntu 18.04开启远程桌面连接&#xff09;&#xff0c;非常简单。但是有小伙伴咨询如何使用微软的远程桌面连接MSTSC&#xff08…

Go栈内存管理源码解读

基本介绍 栈内存一般是由Go编译器自动分配和释放&#xff0c;其中存储着函数的入参和局部变量&#xff0c;这些参数和变量随着函数调用而创建&#xff0c;当调用结束后也会随之被回收。通常开发者不需要关注内存是分配在堆上还是栈上&#xff0c;这部分由编译器在编译阶段通过…

Android Studio学习笔记——广播机制Broadcast

Android Studio学习笔记——广播机制 5.1 广播机制简介5.2 接收系统广播5.2.1 动态注册监听网络变化5.2.2 静态注册实现开机启动 5.3 发送自定义广播5.3.1 发送标准广播5.3.2 发送有序广播 5.4 使用本地广播5.5 广播的最佳实践——强制下线功能 5.1 广播机制简介 安卓每个应用…

spring boot后端开发基础

spring boot后端开发基础 Spring Boot一、开发步骤二、Web分析三、跨域问题四、HTTP协议五、Web服务器六、响应前端请求七、springboot常用注解创建一个简单的RESTful API服务层和数据访问层配置类和Bean定义响应体和路径变量 Spring Boot 一、开发步骤 创建项目 添加依赖 项…

k8s部署Eureka集群

部署有状态负载 镜像配置&#xff1a; 环境变量如下&#xff1a; AUTHENTICATE_ENABLEtrue JAVA_OPTS-Dauth.userName账号 -Dauth.password密码 MY_POD_NAMEmetadata.name BOOL_REGISTERtrue BOOL_FETCHtrue APPLICATION_NAME负载名称 EUREKA_INSTANCE_HOSTNAME${MY_POD_NA…

webpack源码分析——enhanced-resolve库之getType、normalize、join和cachedJoin函数

一、PathType 路径类型 const PathType Object.freeze({Empty: 0, // 空Normal: 1, // 默认值Relative: 2, // 相对路径AbsoluteWin: 3, // win 下的绝对路径AbsolutePosix: 4, // posix 下的绝对路径Internal: 5 // enhanced-resolve 内部自定义的一种类型&#xff0c;具体是…

小程序AI智能名片S2B2C商城系统:做内容、造IP、玩社群打造私域流量的新营销秘籍

在数字化浪潮汹涌的新时代&#xff0c;小程序AI智能名片S2B2C商城系统正以其独特的魅力&#xff0c;引领着营销领域的新变革。这套系统不仅将人工智能与小程序技术完美结合&#xff0c;更通过创新的S2B2C模式&#xff0c;为企业打开了一扇通往成功的大门。 面对激烈的市场竞争&…

SQL注入作业

目录 一、万能密码和二阶注入测试 1.万能密码 2.二阶注入测试 二、联合查询注入测试 1.判断注入点 2.判断当前查询语句的列数 3.查询数据库基本信息 4.查询数据库中的数据 三、报错注入 1. 报错注入函数EXTRATVALUE 2.UPDATEXML 四、盲注测试 1.布尔盲注 判断数据…

Linux搭建Discuz论坛

搭建一个论坛 —接上篇博客 改名/etc/httpd/conf.d/vhosts.conf 》/etc/httpd/conf.d/vhosts.conf.bak [rootlocalhost conf.d]# mv /etc/httpd/conf.d/vhosts.conf /etc/httpd/conf.d/vhosts.conf.bak此时的vhosts.conf是一个新创建的文件&#xff0c;之前的vhosts.conf已经…

使用 Godot 游戏引擎为 Apple 的 visionOS 创建游戏和应用的平台

借助GodotVision ,您可以使用Godot 游戏引擎为 Apple VisionOS创建游戏和应用程序。 保卫牛城堡,一款使用 GodotVision 制作的 VisionOS 游戏 GodotVision 运行一个控制本机RealityKit 视图的无头 Godot实例。粗略地说:Godot 是后端,