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

news2024/9/24 13:15:53

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

第一部分:

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

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


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

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

 

这是控制器代码: 

  private Model2 db = new Model2();

        // GET: admin/Articles
        public 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/5
        public 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/Create
        public 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/5
        public 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/5
        public 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.coloradmin.cn/o/1644940.html

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

相关文章

OpenHarmony实战开发-如何实现动画帧

请求动画帧 请求动画帧时通过requestAnimationFrame函数逐帧回调&#xff0c;在调用该函数时传入一个回调函数。 runframe在调用requestAnimationFrame时传入带有timestamp参数的回调函数step&#xff0c;将step中的timestamp赋予起始的startTime。当timestamp与startTime的差…

分布式与一致性协议之ZAB协议(二)

ZAB协议 ZAB协议是如何实现操作地顺序性的&#xff1f; 如果用一句话解释ZAB协议到底是什么&#xff0c;我觉得它是能保证操作顺序性的、基于主备模式的原子广播协议。 接下来&#xff0c;还是以指令X、Y为例具体演示一下&#xff0c;帮助你更好地理解为什么ZAB协议能实现操作…

8086 汇编学习 Part 8

移位指令 当 C N T > 1 CNT > 1 CNT>1 时&#xff0c;CNT 必须是 CL 寄存器 逻辑左移 SHL OPR , CNT 将寄存器或内存单元中的数据向左移 CNT 位&#xff0c;最后移除的一位写入 CF&#xff0c;最低位用 0 补充 循环左移 ROL OPR , CNT 将寄存器中的值的最高位存…

山海鲸医疗科技:引领智慧医疗新潮流

随着科技的飞速发展&#xff0c;智慧医疗已经成为医疗行业创新的重要方向。在这个背景下&#xff0c;山海鲸智慧医疗解决方案应运而生&#xff0c;以其先进的技术和全面的服务&#xff0c;为医疗行业带来了前所未有的变革。 山海鲸智慧医疗解决方案是一套集成医疗信息化、大数…

三丰云:免费虚拟主机和云服务器的全面评测

引言&#xff1a; 在当前数字化时代&#xff0c;云计算已经成为企业和个人实现在线业务和数据存储的重要方式。然而&#xff0c;选择适合自己需求的云服务提供商并非易事。今天&#xff0c;我们将对三丰云的免费虚拟主机和云服务器进行全面评测&#xff0c;帮助您了解其优势和…

FME学习之旅---day26

我们付出一些成本&#xff0c;时间的或者其他&#xff0c;最终总能收获一些什么。 【由于上周&#xff0c;上班状态不是很好&#xff0c;事情多又杂&#xff0c;没有学习的劲头&#xff0c;就短暂的休息了一下下。双休爬山&#xff0c;给自己上了强度&#xff0c;今天才缓过来…

云商城系统,无后门,一站式系统Java源码

云商城系统&#xff0c;无后门&#xff0c;一站式系统Java源码&#xff0c;心权益商品数量不限数量 系统对接 手动发货 自动发货 兑 换 码 订单监控 商品监控 对象存储 邮箱提醒 加价模板 密价功能 三方支付 会员体系 财务明细 交易分析 售后服务 技术支持 建议配置&#xf…

Flask与HTTP

一、请求响应循环 “请求-响应循环”&#xff1a;客户端发出请求&#xff0c;服务器处理请求并返回响应。 Flask Web程序的工作流程&#xff1a; 当用户访问一个URL&#xff0c;浏览器便生成对应的HTTP请求&#xff0c;经由互联网发送到对应的Web服务器。Web服务器接收请求&a…

本地部署开源白板工具Excalidraw并结合内网穿透远程绘制流程图

文章目录 1. 安装Docker2. 使用Docker拉取Excalidraw镜像3. 创建并启动Excalidraw容器4. 本地连接测试5. 公网远程访问本地Excalidraw5.1 内网穿透工具安装5.2 创建远程连接公网地址5.3 使用固定公网地址远程访问 本文主要介绍如何在Ubuntu系统使用Docker部署开源白板工具Excal…

request库爬取100页tb商品信息

注册账号获取api测试key 1.打开淘宝网站登录并且搜索商品信息。和上一篇文章不一样&#xff0c;这一次我们找到search&#xff1f;这个文件右击复制。 2.在上一篇文章用到的在线curl命令转代码网站将复制过来的东西转换成python代码后&#xff0c;运行得到response。 3.建立cs…

Leetcode—1652. 拆炸弹【简单】

2024每日刷题&#xff08;127&#xff09; Leetcode—1652. 拆炸弹 实现代码 class Solution { public:vector<int> decrypt(vector<int>& code, int k) {int codeSize code.size();vector<int> ans(codeSize, 0);if(k 0) {return ans;}if(k > 0)…

vue2.7与vue2.6、vue3的区别

官网链接&#xff1a;https://v2.cn.vuejs.org/v2/guide/migration-vue-2-7.html -组合式与选项式 选项式&#xff1a;export default { 各种选项关键字名&#xff0c;都定好了&#xff0c;无需引入&#xff0c;配对放置即可}

superset与metabase调研比较

BI工具是什么&#xff1f; 百度百科是这么解释的&#xff1a; 商业智能&#xff08;Business Intelligence&#xff0c;简称&#xff1a;BI&#xff09;&#xff0c;又称商业智慧或商务智能&#xff0c;指用现代数据仓库技术、线上分析处理技术、数据挖掘和数据展现技术进行数…

实施MES管理系统时会遇到的风险以及解决方案

在如今竞争激烈的制造业环境中&#xff0c;每个企业都在努力寻求提升管理效率和降低成本的方法。然而&#xff0c;在引入如ERP系统和MES管理系统等先进管理解决方案时&#xff0c;企业不可避免地会遇到一系列风险。这些风险不仅可能影响项目的成功实施&#xff0c;还可能对企业…

06-xss攻防于绕过

xss的攻击于防御 攻击的利用方式 1&#xff09;获取cookie&#xff0c;实现越权&#xff0c;如果是获取到网站管理员的cookie&#xff0c;也可以叫提权。注意尽量尽快退出账号&#xff0c;删除session&#xff0c;让session失效 2&#xff09;钓鱼网站&#xff0c;模拟真实的…

【前端热门框架【vue框架】】——事件处理与表单输入绑定以及学习技巧,让学习如此简单

&#x1f468;‍&#x1f4bb;个人主页&#xff1a;程序员-曼亿点 &#x1f468;‍&#x1f4bb; hallo 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! &#x1f468;‍&#x1f4bb; 本文由 曼亿点 原创 &#x1f468;‍&#x1f4bb; 收录于专栏&#xff1a…

我们说的数据分析,到底要分析些什么?

作者 Gam 本文为CDA志愿者投稿作品 “我们说数据分析&#xff0c;到底要分析些什么&#xff1f;” 数据分析这个话题自从进入人们的视线以来&#xff0c;这个话题就成为人们茶余饭后的谈资&#xff0c;但是一千个人眼中就有一千个哈姆雷特&#xff0c;就意味着每个人对数据分…

“全国首批EVO+ ICL(V5)临床应用专家”授牌仪式在铭依眼科举行

近日&#xff0c;“全国首批EVO ICL&#xff08;V5&#xff09;新技术临床应用专家”授牌仪式在上海铭依眼科门诊部举行。仪式现场&#xff0c;瑞金医院谢冰教授获得此项荣誉称号。铭依眼科连锁医疗机构创始人吴英、Staar Surgical代表出席仪式现场。 为让近视人群不出国门即可…

AI论文速读 |2024[IJCAI]TrajCL: 稳健轨迹表示:通过因果学习隔离环境混杂因素

题目&#xff1a; Towards Robust Trajectory Representations: Isolating Environmental Confounders with Causal Learning 作者&#xff1a;Kang Luo, Yuanshao Zhu, Wei Chen, Kun Wang(王琨), Zhengyang Zhou(周正阳), Sijie Ruan(阮思捷), Yuxuan Liang(梁宇轩) 机构&a…

什么是水经微图网络加密锁?

水经微图&#xff0c;以下简称“微图”。 我们在《什么是水经微图加密锁&#xff1f;》一文中&#xff0c;为你分享了什么是微图加密锁&#xff0c;以及其使用的方法。 现在&#xff0c;我们再为你分享什么是网络加密锁&#xff0c;以及其使用的方法。 什么是网络加密锁&…