Model :Entity实体模型中的数据组织,对应sql数据库中的表建立相应的类库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Entity
{
/// <summary>
/// 焦点图
/// </summary>
[Serializable]
public partial class FocusImg
{
public FocusImg()
{ }
#region Model
private int _id;
private string _ImgSrc;
private string _Url;
private string _Title;
private DateTime? _BgTime;
private DateTime? _EndTime;
private bool? _State;
#endregion
public int Id
{
set { _id = value; }
get { return _id; }
}
public string ImgSrc
{
set { _ImgSrc = value; }
get { return _ImgSrc; }
}
public string Url
{
set { _Url = value; }
get { return _Url; }
}
public string Title
{
set { _Title = value; }
get { return _Title; }
}
public DateTime? BgTime
{
set { _BgTime = value; }
get { return _BgTime; }
}
public DateTime? EndTime
{
set { _EndTime = value; }
get { return _EndTime; }
}
public bool? State
{
set { _State = value; }
get { return _State; }
}
}
}
//其中 public DataTime? BgTime {} 中的 数据类型加?问号,表示该值可以为空值
在Controllers控制器中,实现FocusImg类与数据库中进行交互
namespace Dal
{
///....
#region 图片上传
public static string jiaodiantu()
{
//SqlHerper类是自定义的数据库交互类,将原本的sql语句进行简化
var dt = SqlHelper.ExecuteDataTable("SELECT * FROM dbo.FocusImg ");
return JsonHelper.DataTableToJSON(dt);
}
///....
}
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Entity;
using Tools;
using Webdiyer.WebControls.Mvc;
using Dal;
namespace WebSite.Controllers
{
public class AdminController : Controller
{
// GET: Admin
public ActionResult Index()
{
return View();
}
public ActionResult jiaodiantu()
{
var json = SiteDal.jiaodiantu();
var fImgData = JsonHelper.JSONToObject<List<FocusImg>>(json);
return View(fImgData);
}
[HttpPost]
public ActionResult SaveJiaoDianTuData(FocusImg data)
{
var postData = data;
string code = postData.ImgSrc;
//base64编码转存成图片
string codeBase64 = code.Split(',')[1];
byte[] bytes = Convert.FromBase64String(codeBase64);
//以时间序列命名图片文件
DateTime now = DateTime.Now;
string dtString = now.Year.ToString()
+ now.Month.ToString().PadLeft(2, '0')
+ now.Day.ToString().PadLeft(2, '0')
+ now.Hour.ToString().PadLeft(2, '0')
+ now.Minute.ToString().PadLeft(2, '0')
+ now.Second.ToString().PadLeft(2, '0')
+ now.Millisecond.ToString().PadLeft(3, '0');
string fileName = dtString + ".jpg";
string filePath = "~/images/Focus/" + fileName;
///映射为物理路径
string fullPath = Server.MapPath(filePath);
System.IO.File.WriteAllBytes(fullPath, bytes);
int state = 0;
if(postData.State==true)
{
state = 1;
}
string cmdText = $@"
INSERT dbo.FocusImg
( ImgSrc ,
Url ,
Title ,
BgTime ,
EndTime ,
State
)
VALUES ( N'{fileName} ' , -- ImgSrc - nvarchar(1000)
N'{postData.Url}' , -- Url - nvarchar(1000)
N'{postData.Title}' , -- Title - nvarchar(1000)
GETDATE() , -- BgTime - datetime
DATEADD(MONTH,1,GETDATE()) , -- EndTime - datetime
{state} -- State - bit
)
";
SqlHelper.ExecuteNonQuery(cmdText);
// img.Save("~/images/Focus/a.jpg");
return Content(" ");
}
}
}
在View视图中,对前端的页面进行编辑cshtml类型文件
@model System.Collections.Generic.List<Entity.FocusImg>
@{
ViewBag.Title = "title";
Layout = "~/Views/Admin/_Layout.cshtml";
}
<script type="text/javascript">