首页 > 其他分享 >.Net Core Mvc (CRUD)Demo

.Net Core Mvc (CRUD)Demo

时间:2022-10-14 11:58:13浏览次数:43  
标签:Core return newsid item Mvc var using Net css

目录

顺手联系了个DEMO

控制器

using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC_Demo_CRUD.Models;

namespace MVC_Demo_CRUD.Controllers
{
    public class NewsController : Controller
    {
        CRUDEntities css = new CRUDEntities();
        // GET: News
        public ActionResult Index()
        {
            ViewBag.msg = (from x in css.Newsinfo
                        join y in css.NewType
                        on x.newsType equals y.typeid
                        select new NewInfos()
                        {
                            newsid = x.newsid,
                            newsName = x.newsName,
                            typeName = y.typeName,
                            typeid=y.typeid,
                            newImage=x.newImage
                        }).ToList();
            return View();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public ActionResult Delete(int newsid)
        {
            var nesid = css.Newsinfo.FirstOrDefault(x => x.newsid == newsid);
            if (nesid!=null)
            {
                css.Newsinfo.Remove(nesid);
                css.SaveChanges();
            }
            return Content("<script>alert('删除完成');window.location.href='http://localhost:9291/News/Index'</script>");
        }
        /// <summary>
        /// 添加视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            var list = css.NewType.ToList();
            return View(list);
        }
        /// <summary>
        /// 添加功能
        /// </summary>
        /// <param name="newsinfos"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Addinfo(NewInfos newsinfos)
        {
            var image = AddImage();
            newsinfos.newImage = image;
            css.Newsinfo.Add(new Newsinfo
            {
                newsName = newsinfos.newsName,
                newsType = newsinfos.typeid,
                newImage = image
            });
            css.SaveChanges();
            return Content("<script>alert('增加完成');window.location.href='http://localhost:9291/News/Add'</script>");
        }
       /// <summary>
       /// 保存图片
       /// </summary>
       /// <returns></returns>
        private string AddImage()
        {
            var guid = "";
            var list = Request.Files.GetMultiple("newImage");
            if (list[0].FileName != "")
            {
                foreach (var item in list)
                {
                    var filename = item.FileName;

                    FileInfo file = new FileInfo(filename);
                    string ext = file.Extension;

                    guid = Guid.NewGuid().ToString() + ext;
                    guid = guid.Replace("-", "");

                    var path = Server.MapPath("~/upload/");
                    item.SaveAs(path + guid);
                }
            }
            return guid;
        }
        /// <summary>
        /// 修改视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit(int newsid)
        {
            //1、当前要修改的类别
            var newsOne = css.Newsinfo.FirstOrDefault(x => x.newsid == newsid);
            //2、类别当前修改的类别
            ViewBag.list2 = css.NewType.FirstOrDefault(x=>x.typeid==newsOne.newsType).typeName;
            //3、所有类别
            ViewBag.list = css.NewType.ToList();
            //4、传递当前编辑
            return View(newsOne);
        }
        /// <summary>
        /// 修改功能
        /// </summary>
        /// <param name="newInfos"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Editinfo(NewInfos newInfos)
        {
            //1、调用保存图片
            var image = AddImage();
            if (newInfos != null)
            {

                //2、保存
                var userinfo = css.Newsinfo.FirstOrDefault(x => x.newsid == newInfos.newsid);
                //1.1查询当前图片为空显示原有

                userinfo.newsName = newInfos.newsName;
                userinfo.newsType = newInfos.typeid;
                //3、如果修改的图片为空则赋值原来的图片
                if (image!="")
                {
                    userinfo.newImage = image;
                }
                else
                {
                    userinfo.newImage=userinfo.newImage;
                }
                css.SaveChanges();
                return Content("<script>alert('修改成功!');window.location.href='http://localhost:9291/News/Index'</script>");
            }
            else
            {
                return Content("<script>alert('修改失败!');window.location.href='http://localhost:9291/News/Edit'</script>");
            }
        }
    }
}

Index页面


@{
    Layout = null;
}
@using MVC_Demo_CRUD.Models
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style>
        img{
            max-width:40px;
            max-height:40px;
        }
    </style>
</head>
<body>
    <div>
        <a href="@Url.Action("Add","News")">添加</a>
        <table border="1" style="border-style: solid;">
            <tbody>
                <tr>
                    <td>编号</td>
                    <td>名称</td>
                    <td>类别</td>
                    <td>图片</td>
                    <td>操作</td>
                </tr>
                @foreach (var item in (List<NewInfos>)ViewBag.msg)
                {
                    <tr>
                        <td>@item.newsid</td>
                        <td>@item.newsName</td>
                        <td id="@item.typeid">@item.typeName</td>
                        <td><img src="http://localhost:9291/Upload/@item.newImage"/></td>
                        <td><a href="@Url.Action("Edit","News",new {item.newsid})">编辑</a> <a href="@Url.Action("Delete","News",new { newsid=item.newsid})">删除</a></td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</body>
</html>

Add 页面


@{
    Layout = null;
}

@using MVC_Demo_CRUD.Models
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Add</title>
</head>
<body>
    <div>
       @*@using(Html.BeginForm("","",FormMethod.Post))
       {
           @Html.TextBox("text")
           @Html.TextBox("File1","",new { type="file"})
           <input type="submit" value="提交"/>
       }*@
        <a href="@Url.Action("Index","News")">首页</a>
        <form action="@Url.Action("Addinfo","News")" method="post" enctype="multipart/form-data">
            新闻名称<input type="text" name="newsName" /><br />
            <select name="typeid">
                @foreach (var item in (List<NewType>)Model)
                {
                    <option value="@item.typeid">@item.typeName</option>
                }
            </select>
            <br />
            图片<input type="file" name="newImage"/><br />
            <input type="submit" value="提交" />
        </form>
    </div>
</body>
</html>

修改页面


@{
    Layout = null;
}
@*@model IEnumerable<MVC_Demo_CRUD.Models.Newsinfo>*@
@using MVC_Demo_CRUD.Models;
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Edit</title>
</head>
<body>
    <div>
        <a href="@Url.Action("Index","News")">首页</a>
        <form action="@Url.Action("Editinfo","News")" method="post" enctype="multipart/form-data">
            <input name="newsid" value="@Model.newsid" type="hidden"/>
            新闻名称<input type="text" name="newsName" value="@Model.newsName"/><br />
                                                                            <select name="typeid">
                                                                                @foreach (var item in (List<NewType>)ViewBag.list)
                                                                                {
                                                                                    //判断当前类别是否选中
                                                                                    if (ViewBag.list2 == item.typeName)
                                                                                    {
                                                                                        <option value="@item.typeid" selected>@item.typeName</option>
                                                                                    }
                                                                                    else
                                                                                    {
                                                                                        <option value="@item.typeid">@item.typeName</option>
                                                                                    }
                                                                                }
                                                                            </select>
            <br />
            图片<input type="file" name="newImage" value="@Model.newImage"/><br />
            <input type="submit" value="提交" />
        </form>
    </div>
</body>
</html>

最后效果

标签:Core,return,newsid,item,Mvc,var,using,Net,css
From: https://www.cnblogs.com/Bo-H/p/16791155.html

相关文章

  • .net maui拨打电话
    Platforms》Android》AndroidManifest.xml中加配置<manifestxmlns:android="http://schemas.android.com/apk/res/android"> <applicationandroid:allowBackup="true"......
  • Java每日一题之Netty高性能表现在那些方面?
    Java每日一题之Netty高性能表现在那些方面?答案:(1)IO线程模型:同步非阻塞,用最少的资源做更多的事情。(2)内存零拷贝:尽量减少不必要的内存拷贝,实现了更高效率的传输。(......
  • SpringMVC学习笔记
    13120171114......
  • Mounting Kubernetes config map as single file returns error: "caused: mount thro
    原因是k8s的configmap不能挂载文件,只能挂载文件夹本来以为自己的挂载的是文件夹后面发现docker打包时添加文件命令文件夹没有加"/"导致被打成文件了add./index.html......
  • Kubernetes入门宝典
    docker基础在线安装1、docker安装centos7sudoyumremovedocker\docker-client\docker-client-latest\......
  • .net6 WebApi 之 Configuration
    .net6WebApi使用Configuration获取appsettings.json中的内容,用法如下:appsettings.json中的内容{"Logging":{"LogLevel":{"Default":"Informati......
  • 看一遍就理解:MVCC原理详解
    前言MVCC实现原理是一道非常高频的面试题,最近技术讨论群的小伙伴一直在讨论,趁着国庆节有空,我们一起来聊聊。1.相关数据库知识点回顾1.1什么是数据库事务,为什么要有事务......
  • 【VB.NET】创建Forms项目无设计界面的解决办法
    使用VisualStudio2022开发工具创建VB.NET窗体应用程序项目模板的步骤如下:1.打开VisualStudio后,点击[文件]菜单,点击[创建项目]。2.在弹出的创建新项目窗口中,选择顶部的......
  • .NET6接入Skywalking链路追踪
    .NET6接入Skywalking链路追踪完整流程 一、Skywalking介绍Skywalking是一款分布式链路追踪组件,什么是链路追踪?随着微服务架构的流行,服务按照不同的维度进行拆分,一次......
  • 记一次 .NET 某企业OA后端服务 卡死分析
    一:背景1.讲故事前段时间有位朋友微信找到我,说他生产机器上的Console服务看起来像是卡死了,也不生成日志,对方也收不到我的httpclient请求,不知道程序出现什么情况了,特来寻......