C#.Net前端数据post传递方法(mvc)

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">
  
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值