asp.net朱勇项目个人博客(3)

引文:按照书上的项目,我们最后实现管理端的三个增删改查的功能即可,相对与三个增删改查,文章,分类和留言,这里我们所需要用的的关联的一个表就是文章表,因为文章表每一个文章的增加显示和修改都需要对应的一个分类,也就是这三个增删改查稍微复杂的一个点.
 

第一部分:

我们首先来实现文章的增删改查功能:

首先我们添加控制器,按照我的步骤添加如下的控制器。


在控制部分需要修改的代码就是这个一个,我们需要有一个分页的实现,然后还要有一个关于Category的一个Id关联,

然后在创建和编辑的时候我们也需要对代码进行一下关联的修改 

 

这是控制器代码: 

  private Model2 db = new Model2();// GET: admin/Articlespublic ActionResult Index(int? page){int pageIndex = page ?? 1;int pageSize = 2; var orderedArticles = db.Articles.OrderBy(a => a.Id).Include(a=>a.Category); // 假设按 Id 升序排序IPagedList<Article> p1 = orderedArticles.ToPagedList(pageIndex, pageSize);return View(p1);}// GET: admin/Articles/Details/5public ActionResult Details(int? id){if (id == null){return new HttpStatusCodeResult(HttpStatusCode.BadRequest);}Article article = db.Articles.Find(id);if (article == null){return HttpNotFound();}return View(article);}// GET: admin/Articles/Createpublic ActionResult Create(){ViewBag.Category_Id = new SelectList(db.Categories,"Id","Name");return View();}[HttpPost][ValidateAntiForgeryToken]public ActionResult Create([Bind(Include = "Id,Title,Content,CategoryName,addDate,Hit,Category_Id")] Article article){if (ModelState.IsValid){article.addDate= DateTime.Now;db.Articles.Add(article);db.SaveChanges();return RedirectToAction("Index");}ViewBag.Category_Id = new SelectList(db.Categories, "Id", "Name",article.Category_Id);return View(article);}// GET: admin/Articles/Edit/5public ActionResult Edit(int? id){if (id == null){return new HttpStatusCodeResult(HttpStatusCode.BadRequest);}Article article = db.Articles.Find(id);if (article == null){return HttpNotFound();}ViewBag.Category_Id = new SelectList(db.Categories, "Id", "Name", article.Category_Id);return View(article);}// POST: admin/Articles/Edit/5// 为了防止“过多发布”攻击,请启用要绑定到的特定属性;有关// 更多详细信息,请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。[HttpPost][ValidateAntiForgeryToken]public ActionResult Edit([Bind(Include = "Id,Title,Content,CategoryName,addDate,Hit,Category_Id")] Article article){if (ModelState.IsValid){article.addDate = DateTime.Now;db.Entry(article).State = EntityState.Modified;db.SaveChanges();return RedirectToAction("Index");}ViewBag.Category_Id = new SelectList(db.Categories, "Id", "Name", article.Category_Id);return View(article);}// GET: admin/Articles/Delete/5public ActionResult Delete(int? id){if (id == null){return new HttpStatusCodeResult(HttpStatusCode.BadRequest);}Article article = db.Articles.Find(id);if (article == null){return HttpNotFound();}return View(article);}// POST: admin/Articles/Delete/5[HttpPost, ActionName("Delete")][ValidateAntiForgeryToken]public ActionResult DeleteConfirmed(int id){Article article = db.Articles.Find(id);db.Articles.Remove(article);db.SaveChanges();return RedirectToAction("Index");}protected override void Dispose(bool disposing){if (disposing){db.Dispose();}base.Dispose(disposing);}}

 然后这个是列表显示页的视图代码

<p class="p">@Html.ActionLink("创建文章", "Create")
</p>
<table class="table"><tr><th>@*@Html.DisplayNameFor(model => model.Title)*@标题</th><th>@* @Html.DisplayNameFor(model => model.Content)*@内容</th><th>@*@Html.DisplayNameFor(model => model.addDate)*@添加日期</th><th>@* @Html.DisplayNameFor(model => model.Hit)*@浏览量</th><th>@* @Html.DisplayNameFor(model => model.Category_Id)*@分类</th><th></th></tr>@foreach (var item in Model){<tr><td>@Html.DisplayFor(modelItem => item.Title)</td><td class="content-width">@Html.DisplayFor(modelItem => item.Content)</td><td>@Html.DisplayFor(modelItem => item.addDate)</td><td>@Html.DisplayFor(modelItem => item.Hit)</td><td>@Html.DisplayFor(modelItem => item.Category.Name)</td><td>@Html.ActionLink("编辑", "Edit", new { id = item.Id }) |@Html.ActionLink("详情", "Details", new { id = item.Id }) |@Html.ActionLink("删除", "Delete", new { id = item.Id })</td></tr>}</table>
<div class="pager">@Html.PagedListPager(Model, page => Url.Action("Index", new { page }),PagedListRenderOptions.ClassicPlusFirstAndLast)
</div>

这个是创建页的控制器代码 


<h2>新建文章</h2>@using (Html.BeginForm()) 
{@Html.AntiForgeryToken()<div class="form-horizontal"><hr />@Html.ValidationSummary(true, "", new { @class = "text-danger" })<div class="form-group">@Html.Label("文章标题", htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-10">@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })</div></div><div class="form-group">@Html.Label("文章内容", htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-10">@Html.TextAreaFor(model => model.Content, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" })</div></div><div class="form-group">@Html.Label("分类", htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-10">@Html.DropDownList("Category_Id", String.Empty)@Html.ValidationMessageFor(model => model.Category_Id, "", new { @class = "text-danger" })</div></div><div class="form-group"><div class="col-md-offset-2 col-md-10"><input type="submit" value="保存" class="btn btn-default" /></div></div></div>
}<div>@Html.ActionLink("返回列表", "Index")
</div>

这是编辑页的控制器代码:
 


<h2>文章编辑</h2>@using (Html.BeginForm())
{@Html.AntiForgeryToken()<div class="form-horizontal"><div class="form-group">@Html.Label("标题", htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-10">@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })</div></div><div class="form-group">@Html.Label("文章内容", htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-10">@Html.TextAreaFor(model => model.Content, new { htmlAttributes = new { @class = "form-control" } })@Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" })</div></div><div class="form-group">@Html.Label("分类", htmlAttributes: new { @class = "control-label col-md-2" })<div class="col-md-10">@Html.DropDownList("Category_Id", String.Empty)@Html.ValidationMessageFor(model => model.Category_Id, "", new { @class = "text-danger" })</div></div><div class="form-group"><div class="col-md-offset-2 col-md-10"><input type="submit" value="保存" class="btn btn-default" /></div></div>
</div>
}<div>@Html.ActionLink("Back to List", "Index")
</div>

然后剩下的两个增删改查就不需要做过多的操作,只需要按视图模型生成即可 

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

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

相关文章

【Linux】网络连接配置——nmcli工具配置连接增删改查实例

nmcli工具配置连接增删改查实例 &#xff08;一&#xff09;网络连接配置基本项目1.网络接口配置2.主机名配置3.DNS服务器配置 &#xff08;二&#xff09;网络连接配置文件&#xff08;三&#xff09;网络配置方法&#xff08;四&#xff09;nmcli工具配置连接管理1.增2.查3.改…

sql编写规范(word原件)

编写本文档的目的是保证在开发过程中产出高效、格式统一、易阅读、易维护的SQL代码。 1 编写目的 2 SQL书写规范 3 SQL编写原则 软件全套资料获取进主页或者本文末个人名片直接获取。

Spring框架Aware接口的作用和应用

Aware&#xff0c;这是一个空接口&#xff0c;空接口又称标记接口。标记接口的作用是实现该接口的类都被标记具有某项功能。 一个超级标记接口&#xff0c;指示一个Bean有资格通过回调式方法被Spring容器通知某个特定框架对象。具体的方法签名由各个子接口确定&#xff0c;但通…

java设计模式四 桥接模式

桥接模式关注于将抽象部分与实现部分分离&#xff0c;使它们可以独立变化。它通过在抽象和实现之间建立一个桥梁来实现这一目的。这种设计模式属于结构型模式。 假设我们要设计一个图形编辑器&#xff0c;其中图形&#xff08;如圆形、正方形&#xff09;可以有不同的颜色填充…

GStreamer日志调试笔记

1、查询所有分类 #gst-launch-1.0 --gst-debug-help 2、查询videotestsrc的日志 #gst-launch-1.0 --gst-debug-help | findstr videotestsrc 结果&#xff1a; 3、使用--gst-debug设置相应日志类型的相应等级&#xff0c;越大显示日志越多&#xff0c;排查内存泄露可以设置为9 …

国内外主流大模型都具备有哪些特点?

文章目录 ⭐ 火爆全网的大模型起点⭐ 国外主流LLM及其特点⭐ 国内主流LLM及其特点⭐ 全球大模型生态的发展 该章节呢&#xff0c;我们主要是看一下关于国内外主流的大语言模型&#xff0c;通过它们都具备哪些特点&#xff0c;来达成对多模型有一个清晰的认知。对于 “多模型” …

计网作业0429

1.关于现有IPv4分片/重组机制&#xff0c;描述正确的是&#xff08; &#xff09; A.可以在源主机分片 B.可以在中间路由器分片 C.可以在目的主机重组 D.不可以在中间路由器重组 2. 普通路由器转发&#xff08;非NAT&#xff09;IP报文过程中&#xff0c;IP报文中哪些字段会…

帮助命令

1.man 原意&#xff1a;manual 所在路径&#xff1a;/usr/bin/man 执行权限&#xff1a;所有用户 语法&#xff1a;man [命令或配置文件] 功能描述&#xff1a;获得帮助信息 例&#xff1a;$ man ls 查看ls命令的帮助信息 查看命令的帮助主要是看这个命令是干什么用的&am…

Vue3+.NET6前后端分离式管理后台实战(十七)

1&#xff0c;Vue3.NET6前后端分离式管理后台实战(十七)已经在微信公众号更新&#xff0c;有兴趣的扫码关注一起交流学习。

数据治理:数据孤岛是企业信息化发展中难以避免的阶段

随着信息技术的飞速发展&#xff0c;企业对于数据的依赖程度日益加深。在这个过程中&#xff0c;数据治理成为了企业信息化建设的核心环节。然而&#xff0c;在实际操作中&#xff0c;企业往往会遇到一种难以避免的现象——数据孤岛。 一、数据孤岛的定义与成因 数据孤岛&…

电磁兼容(EMC):产品适用静电放电(ESD)标准全解

目录 1. 标准体系 2. 试验方法标准 3. 常见产品的抗扰度标准 自己研发的产品到底需要满足什么样的静电放电标准要求才是满足国家标准要求。客户提出的静电放电接触放电4kV&#xff0c;空气放电8kV要求&#xff0c;是高于国家标准要求还是低于国家标准要求&#xff1f;面对这…

PyCharm 2024新版图文安装教程(python环境搭建+PyCharm安装+运行测试+汉化+背景图设置)

名人说&#xff1a;一点浩然气&#xff0c;千里快哉风。—— 苏轼《水调歌头》 创作者&#xff1a;Code_流苏(CSDN) 目录 一、Python环境搭建二、PyCharm下载及安装三、解释器配置及项目测试四、PyCharm汉化五、背景图设置 很高兴你打开了这篇博客&#xff0c;如有疑问&#x…

类的流插入与流提取

1.自定义类型不能直接使用流插入与流提取 为什么内置类型可以直接使用流插入与流提取&#xff1f; 其实本质上是人家已经写好了相关的函数&#xff0c;内置类型可以直接调用cout与cin 对于自定义类型&#xff0c;我们需要自己写相关的函数。 2.解决方法 2.1自己在类里面写一个…

微信小程序 【关键部分】

1. 动机 最近在开发小程序&#xff0c;小程序既需兼顾针对新用户的内容预览&#xff0c;又要为注册用户提供服务&#xff0c;简单梳理下&#xff0c;基本需求如下&#xff1a; 小程序共三个tab页&#xff0c;所有用户都可以浏览首页内容&#xff0c;了解我们可以提供的优质服…

Microsoft Remote Desktop Beta for Mac:远程办公桌面连接工具

Microsoft Remote Desktop Beta for Mac不仅是一款远程桌面连接工具&#xff0c;更是开启远程办公新篇章的利器。 它让Mac用户能够轻松访问和操作远程Windows计算机&#xff0c;实现跨平台办公的无缝衔接。无论是在家中、咖啡店还是旅途中&#xff0c;只要有网络连接&#xff0…

鸿蒙UI复用

鸿蒙UI复用 简介BuilderBuilder的使用方式一Builder的使用方式二Builder的使用方式三 Component使用Component复用UI 简介 在页面开发过程中&#xff0c;会遇到有UI相似的结构&#xff0c;如果每个UI都单独声明一份&#xff0c;会产生大量冗余代码&#xff0c;不利于阅读。遇到…

Python头歌合集(题集附解)

目录 一、Python初识-基本语法 第1关&#xff1a;Hello Python! 第2关&#xff1a;我想看世界 第3关&#xff1a;学好Python 第4关&#xff1a;根据圆的半径计算周长和面积 第5关&#xff1a;货币转换 二、turtle简单绘图 第1关&#xff1a;英寸与厘米转换 第2关&#xff1…

鸿蒙应用开发系列 篇二:鸿蒙系统开发工具与环境

文章目录 系列文章硬件与软件需求DevEco Studio扩展工具与框架开发资源系列文章 鸿蒙应用开发系列 篇一:鸿蒙系统概述 鸿蒙应用开发系列 篇二:鸿蒙系统开发工具与环境 (系列计划预告) 鸿蒙系统UI/UX设计 鸿蒙系统应用开发基础 鸿蒙系统高级开发技术 鸿蒙系统特色功能开发 …

线程池和CountDownLatch搭配使用

一&#xff0c;CountDownLatch CountDownLatch是Java并发编程中用于线程间协调的一个同步辅助类。它通过一个初始计数值来控制线程的等待&#xff0c;这个计数值在其他线程执行特定任务时递减。 初始化&#xff1a;创建CountDownLatch实例时&#xff0c;你需要指定一个整数值&…

vue-cli+vue3+vite+ts 搭建uniapp项目全过程(一)

unapp官方提供了cli 脚手架创建 uni-app 项目的文档 Vue3/Vite版要求 node 版本 18、20使用Vue3/Vite版创建不会提示选择模板&#xff0c;目前只支持创建默认模板 本文以vue3vitets为例 1、初始化项目 npx degit dcloudio/uni-preset-vue#vite-ts my-vue3-project 执行完生成…