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

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

第一部分:

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

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


在控制部分需要修改的代码就是这个一个,我们需要有一个分页的实现,然后还要有一个关于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.mfbz.cn/a/594070.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Spring入门及注解开发

1 引言 自定义注解可以用来为代码添加元数据信息,简化配置,提高代码的可读性和可维护性。通过自定义注解,可以实现自定义的业务逻辑、约束条件、配置参数等功能。在Spring中,自定义注解常用于标记组件、配置依赖注入、AOP切面等。 自定义注解可以添加元数据信息,低代码框…

银行智能化数据安全分类分级实践分享

文章目录 前言一、数据安全智能分类分级平台建设背景二、数据安全分类分级建设思路和实践1、做标签– 数据安全标签体系2、打标签– 鹰眼智能打标平台 3.03、用标签– 全行统一“数据安全打标签结果”服务提供前言 随着国家对数据安全的高度重视,以及相关法律法规的出台,数据…

【linuxC语言】stat函数

文章目录 前言一、stat函数二、示例代码总结 前言 在Linux系统编程中&#xff0c;stat() 函数是一个非常重要的工具&#xff0c;用于获取文件的元数据信息。无论是在系统管理、文件处理还是应用开发中&#xff0c;都可能会用到 stat() 函数。通过调用 stat() 函数&#xff0c;…

ue引擎游戏开发笔记(31)——对角色移动进行优化:角色滑步处理

1.需求分析&#xff1a; 角色的移动与动画不匹配&#xff0c;角色移动起来像是在滑行。。。适当进行优化。 2.操作实现&#xff1a; 这个问题本质是角色的运动速度并没有匹配世界动画的运行速度&#xff0c;不论世界动画快慢于角色移动速度&#xff0c;都会感到有滑步感。所以…

基于 Spring Boot 博客系统开发(六)

基于 Spring Boot 博客系统开发&#xff08;六&#xff09; 本系统是简易的个人博客系统开发&#xff0c;为了更加熟练地掌握 SprIng Boot 框架及相关技术的使用。&#x1f33f;&#x1f33f;&#x1f33f; 基于 Spring Boot 博客系统开发&#xff08;五&#xff09;&#x1f…

适合打工人的赚钱软件有哪些?盘点5个实用的赚钱软件(真实靠谱)

在这个互联网时代&#xff0c;手机不仅仅是我们的通讯工具&#xff0c;更是我们赚钱的小助手。今天&#xff0c;就让我带你一探究竟&#xff0c;揭秘那些真实靠谱的赚钱软件&#xff0c;让你在家也能轻松赚钱&#xff01; 一、抖音极速版&#xff1a;刷视频也能赚钱 抖音极速版…

Flutter笔记:Widgets Easier组件库(11)- 使用提示吐丝

Flutter笔记 Widgets Easier组件库&#xff08;11&#xff09;使用提示吐丝 - 文章信息 - Author: 李俊才 (jcLee95) Visit me at CSDN: https://jclee95.blog.csdn.netMy WebSite&#xff1a;http://thispage.tech/Email: 291148484163.com. Shenzhen ChinaAddress of this …

【前端学习——网络相关】浏览器同源策略和跨域

浏览器的同源策略 为什么要有&#xff1f; 帮助阻隔恶意文档&#xff0c;减少可能被攻击的媒介。&#xff08;就是为了安全&#xff09; 如果非同源&#xff0c;共有三种行为受到限制 &#xff08;1&#xff09; Cookie、LocalStorage 和 IndexDB 无法读取。 &#xff08;2…

央视影音 视频下载 2

浏览器猫抓插件&#xff0c;拿到视频地址&#xff0c;这个地址的播放不正常&#xff0c;花屏。https://dh5.cntv.qcloudcdn.com/asp/h5e/hls/2000/0303000a/3/default/6edd15a0ebb3467993bec51a95be0e22/2000.m3u8 改一下地址&#xff0c;把代码中的h5e去掉。网址改为https://…

解决MySQL进行group by 字段返回大量异常结果

目录 问题 原因 解决方案 问题 看这条sql CH2O这个字段的取值只有1&#xff0c;2&#xff0c;3&#xff0c;正常进行group by 分类累加统计返回结果应该是这样&#xff1a; [{"CH2O": 2.0,"insufficient_weight": 142,"Normal_Weight": 164…

锁相环原理解析

在计算机和嵌入式系统中&#xff0c;常常要用锁相环来倍频&#xff0c;那么&#xff0c;锁相环是如何倍频的&#xff0c;其原理又是什么呢&#xff1f; 目录 1. 锁相环基本概念与构成1.1 鉴相器1.2 低通滤波器1.3 压控振荡器 2. 锁相环如何实现倍频3. 锁相环也会失效&#xff…

基于springboot+vue+Mysql的租房网站

开发语言&#xff1a;Java框架&#xff1a;springbootJDK版本&#xff1a;JDK1.8服务器&#xff1a;tomcat7数据库&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09;数据库工具&#xff1a;Navicat11开发软件&#xff1a;eclipse/myeclipse/ideaMaven包&#xff1a;…

[C++核心编程-01]----C++内存四区详细解析

前言 当程序运行时&#xff0c;操作系统会为程序分配一块内存空间&#xff0c;这块内存空间被划分为不同的区域&#xff0c;每个区域有其独特的作用和管理方式。四个区域分别为&#xff1a;堆、栈、全局/静态存储区和常量存储区。每个区域都有不同的作用和特点&#xf…

夏目友人帐所有妖怪名单

夏目友人帐妖怪名单 夏目友人帐 第一季 2008.07.07第1话&#xff1a;猫和友人帐 / 猫と友人帐 菱垣 狞影 斑第2话&#xff1a;露神之祠 / 露神の祠 露神 濯第3话&#xff1a;八原的怪人 / 八ツ原の怪人 一只目 牛头&#xff08;中级妖怪&#xff09;第4话&#xff1a;时雨与少女…

PHP源码_众筹商城

众筹商城源码 众筹商品平台 商城加共识元富之路 网上商城众筹 前端是编译后的&#xff0c;后端PHP&#xff0c;带商城 运行截图 源码贡献 https://githubs.xyz/boot?app39 部分数据库表 CREATE TABLE ti_shopro_store (id int(11) NOT NULL AUTO_INCREMENT COMMENT ID,nam…

英语复习之英语形近词总结(二)

接着总结形近词 单词释义例句 impress 英 /ɪmˈpres/ 美 /ɪmˈpres/ vt.盖印&#xff1b;强征&#xff1b;传送&#xff1b;给予某人深刻印象 vi.给人印象。印象&#xff0c;印记&#xff1b;特征&#xff0c;痕迹 1.It didnt impress me as a good place to live. 那地方…

基于openEuler22.03 LTS环境的docker容器基础

一、说明 本文配置环境为VMware虚拟机或华为云服务器&#xff08;4核CPU&#xff0c;8 GB内存&#xff0c;40GB磁盘&#xff09;&#xff0c;OS为openEuler 22.03 LTS &#xff0c;Linux服务器要求能联网。 二、安装docker 2.1 安装docker软件包 [rootnode01 ~]# dnf -y in…

电机控制系列模块解析(15)—— 母线小电容

一、薄膜电容 在家电产品和工业变频器中&#xff0c;使用容值更小但耐压更高的薄膜电容来代替传统的电解电容作为逆变器母线电容&#xff0c;这种技术趋势已经得到了广泛应用和产品化。以下是关于这一替换技术的一些关键考量和优势&#xff1a; 长期稳定性与可靠性&#xff1a…

3-qt综合实例-贪吃蛇的游戏程序

引言&#xff1a; 如题&#xff0c;本次实践课程主要讲解贪吃蛇游戏程序。 qt贪吃蛇项目内容&#xff1a; 一、功能需求 二、界面设计 各组件使用&#xff1a; 对象名 类 说明 Widget QWidge 主窗体 btnRank QPushButton 排行榜-按钮 groupBox QGroupBox 难…

牛客NC383 主持人调度(一)【简单 排序 Java/Go/C++】

题目 题目链接&#xff1a; https://www.nowcoder.com/practice/e160b104354649b69600803184094adb 思路 直接看代码&#xff0c;不难Java代码 import java.util.*;public class Solution {/*** 代码中的类名、方法名、参数名已经指定&#xff0c;请勿修改&#xff0c;直接返…
最新文章