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.hqwc.cn/news/669997.html

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

相关文章

数据结构-AVL树

目录 什么是 AVL 树 ASL 度量查找效率 结构体定义 平衡调整 调整类型 左旋和右旋 右旋 左旋 左、右平衡调整 左平衡调整 右平衡调整 插入数据 模拟建立 AVL 树 什么是 AVL 树 二叉排序树的形状取决于数据集&#xff0c;当二叉树的高度越小、结构越合理&#xff0c…

Java请求第三方接口的一些步骤

一、前言 Java请求第三方接口的一些步骤。 在Java中请求第三方接口通常涉及以下步骤。这些步骤涵盖了从准备请求到处理响应的整个过程。 1. 确定接口详情 接口URL&#xff1a;你要请求的URL。请求方法&#xff1a;如GET、POST、PUT、DELETE等。请求参数&#xff1a;包括URL…

机器人操作系统ROS2学习 1

随着智能化时代的进程&#xff0c;机器人也在向着高度智能化的方向发展&#xff0c;这对应的机器人操作系统也就相应而生了。机器人操作系统ROS (Robot Operating System)的诞生为机器人系统的开发与应用带来了很大方便&#xff0c;也聚集了全球大量的应用开发人员推动ROS的发展…

Debian是什么?有哪些常用命令

目录 一、Debian是什么&#xff1f; 二、Debian常用命令 三、Debian和CentOS的区别 四、Debian和CentOS的优缺点 五、Debian和CentOS的运用场景 一、Debian是什么&#xff1f; Debian是一种流行的开源Linux操作系统。 Debian是一个以Linux内核为基础的操…

【Git】Github创建远程仓库并与本地互联

创建仓库 点击生成新的仓库 创建成功后会生成一个这样的文件 拉取到本地 首先先确保本地安装了git 可以通过终端使用 git --version来查看是否安装好了git 如果显示了版本信息&#xff0c;说明已经安装好了git&#xff0c;这时候我们就可以进入我们想要clone到问目标文件夹 …

Prometheus Metrics指标类型 Histogram、Summary分析数据分布情况

​​​Histogram 直方图 、Summary 摘要 使用Histogram和Summary分析数据分布情况 除了 Counter 和 Gauge 类型的监控指标以外&#xff0c;Prometheus 还定义了 Histogram 和 Summary 的指标类型。Histogram 和 Summary 主用用于统计和分析样本的分布情况。 在大多数情况下人们…

数据库大作业——基于qt开发的图书管理系统 (一)环境的配置与项目需求的分析

前言 博主最近数据库原理结课要做课程设计了,要求开发基于数据库实现的图书管理系统&#xff0c;博主想了想决定做一个基于Qt的图书管理系统,博主在此之前其实也没有用过多少Qt&#xff0c;仅以此专栏记录博主学习与开发的全过程&#xff0c;大家一起学习&#xff0c;一起进步…

[leetcode] 64. 最小路径和

文章目录 题目描述解题方法动态规划java代码复杂度分析 相似题目 题目描述 给定一个包含非负整数的 m x n 网格 grid &#xff0c;请找出一条从左上角到右下角的路径&#xff0c;使得路径上的数字总和为最小。 说明&#xff1a;每次只能向下或者向右移动一步。 示例 1&#…

区块链开发用的是哪种编程语言?

区块链技术作为近年来备受瞩目的新兴技术之一&#xff0c;其核心的特性之一就是去中心化、安全性高、透明度高和可扩展性强。而区块链的开发语言则是实现这一技术的关键因素之一。那么&#xff0c;区块链开发语言是哪一种编程语言呢&#xff1f; 一、区块链开发语言的特点和选…

Whisper、Voice Engine推出后,训练语音大模型的高质量数据去哪里找?

近期&#xff0c;OpenAI 在语音领域又带给我们惊喜&#xff0c;通过文本输入以及一段 15 秒的音频示例&#xff0c;可以生成既自然又与原声极为接近的语音。值得注意的是&#xff0c;即使是小模型&#xff0c;只需一个 15 秒的样本&#xff0c;也能创造出富有情感且逼真的声音。…

.[[MyFile@waifu.club]].svh勒索病毒数据库恢复方案

.[[MyFilewaifu.club]].svh勒索病毒有什么特点&#xff1f; .[[MyFilewaifu.club]].svh是一种最近多发的勒索病毒&#xff0c;它通过加密受害者的文件并要求支付赎金来解锁&#xff0c;从而达到勒索钱财的目的。恢复重要数据请添加技术服务号(safe130)。以下是关于这种病毒的详…

同创优配正规股票港股市场炒股加仓!中国资产,火了

查查配近日,受国内利好政策不断、全球资金对中国市场信心提升等多重因素影响,市场风险偏好明显上升,A股、港股市场均表现强劲,上证指数站上3100点,恒生指数也上演九连阳,创下2018年以来的最长连涨纪录。从资金动向来看,机构加仓明显,北向资金持续净买入,A股ETF也被大幅加仓,部分…