//MVC 具体方法
//API地址 通过 WebConfig配置 private static string apiAdds = ConfigurationManager.AppSettings["ApiAddress"]; //具体方法 public int AddSelectFlowerBll(string selectProduct, string productName, string productSum,int UserID) { try { //非空判断 if (!string.IsNullOrEmpty(selectProduct) && !string.IsNullOrEmpty(productName) && !string.IsNullOrEmpty(productSum)) { AssembleFlower aFlower = new AssembleFlower(); aFlower.Name = productName; aFlower.PriceSum = Convert.ToInt32(productSum); //添加人为1 aFlower.AssembleMan = UserID; aFlower.AssembleTime = DateTime.Now; aFlower.IsDel = 0; List<AssembleMaterial> materialList = new List<AssembleMaterial>(); string[] productArry = selectProduct.Split(';'); for (int i = 0; i < productArry.Length - 1; i++) { string[] flowerArry = productArry[i].Split(','); AssembleMaterial m = new AssembleMaterial(); m.MaterialID = Convert.ToInt32(flowerArry[0]); m.MaterialCount = Convert.ToInt32(flowerArry[1]); materialList.Add(m); } string[] strArray = { JsonConvert.SerializeObject(aFlower), JsonConvert.SerializeObject(materialList) }; #region 向后台提交数据 //创建HttpClient对象 Uri uri = new Uri(apiAdds); HttpClient client = new HttpClient(); client.BaseAddress = uri; client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var jsonStr = JsonConvert.SerializeObject(strArray); HttpContent cont = new StringContent(jsonStr); cont.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var returnStr = ""; HttpResponseMessage resp = client.PostAsync("api/SelectFlower/AssembleAdd", cont).Result;//post提交数据, if (resp.IsSuccessStatusCode) { returnStr = resp.Content.ReadAsStringAsync().Result; } #endregion return Convert.ToInt32(JsonConvert.DeserializeObject(returnStr)); } else { //空值 return -1; } } catch { //发生错误 return -1; throw; } }
//API [RoutePrefix("api/SelectFlower")] public class SelectFlowerController : ApiController { /// <summary> /// 获取自选花的类型 /// </summary> /// <param name="FlowerType"></param> /// <returns></returns> [HttpGet] [Route("GetSelectFlower")] public DataTable GetSelectFlower(string FlowerType) { string sql = string.Format("select * from FlowerMaterial where IsDel=0 and FlowerType=@FlowerType"); SqlParameter parameter = new SqlParameter("@FlowerType", SqlDbType.VarChar, 200); parameter.Value = FlowerType; return DBHelperSQL.QueryDataTable(sql, parameter); } [HttpPost] [Route("AssembleAdd")] public string AssembleAdd([FromBody] dynamic jsonStr) { //将前台传过来的值转化为数组 var data = JsonConvert.DeserializeObject<string[]>(jsonStr.ToString()); AssembleFlower aFlower = JsonConvert.DeserializeObject<AssembleFlower>(data[0]); List<AssembleMaterial> MaterialList = JsonConvert.DeserializeObject<List<AssembleMaterial>>(data[1]); List<DBHelperSQL.KeyValue> list=new List<DBHelperSQL.KeyValue>(); list.Add(new DBHelperSQL.KeyValue() { Key = "insert into AssembleFlower values(@Name,@PriceSum,@AssembleMan,@AssembleTime,0)", Value = new SqlParameter[] { new SqlParameter("@Name",aFlower.Name), new SqlParameter("@PriceSum",aFlower.PriceSum), new SqlParameter("@AssembleMan",aFlower.AssembleMan), new SqlParameter("@AssembleTime",aFlower.AssembleTime), } }); foreach (var material in MaterialList) { list.Add(new DBHelperSQL.KeyValue() { Key = "insert into AssembleMaterial values(@MaterialID,@MaterialCount,(select top 1 ID from AssembleFlower order by ID desc))", Value = new SqlParameter[] { new SqlParameter("@MaterialID",material.MaterialID), new SqlParameter("@MaterialCount",material.MaterialCount) } }); } list.Add(new DBHelperSQL.KeyValue() { Key = " insert into MyCar values(3,(select top 1 ID from AssembleFlower order by ID desc),1,@UserID,@CreateTime,0)", Value = new SqlParameter[] { new SqlParameter("@UserID",aFlower.AssembleMan), new SqlParameter("@CreateTime",DateTime.Now) } }); return DBHelperSQL.ExecuteSqlTranAndReturn(list).ToString(); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)