コード例 #1
0
        public async Task <IActionResult> Upsert(University obj)
        {
            string token = HttpContext.Session.GetString("JWToken");

            if (ModelState.IsValid)
            {
                var files = HttpContext.Request.Form.Files;
                if (files.Count > 0)
                {
                    byte[] pic = null;
                    using (var fs = files[0].OpenReadStream())
                    {
                        using (var ms = new MemoryStream())
                        {
                            fs.CopyTo(ms);
                            pic = ms.ToArray();
                        }
                    }
                    obj.Picture = pic;
                }
                else if (obj.Id == 0)
                {
                    // If we're creating but not choose any Picture
                    obj.Picture = null;
                }
                else
                {
                    //Reuse old picture if we're updating and not choose any Picture
                    var objFromDb = await _db.GetAsync(_url, obj.Id, token);

                    obj.Picture = objFromDb.Picture;
                }

                //Create
                if (obj.Id == 0)
                {
                    await _db.CreateAsync(_url, obj, token);
                }
                //Update
                else
                {
                    await _db.UpdateAsync(_url, obj.Id, obj, token);
                }
                TempData["Alert"] = "Modify Successfully !";
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(View(obj));
            }
        }