[toc]最近没事复习了下MVC
顺手联系了个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>
最后效果