MVC控制器if / else语句

时间:2018-04-06 15:37:10

标签: c# asp.net-mvc entity-framework if-statement controller

我有一个提交数据的MVC表单。提交后,它会进入预览视图表单,用户可以决定是继续发布还是再次编辑表单。

除了提交数据外,我还提供上传图片的选项。我在此预览表单中基本上有3个场景:

  1. 更新表格并保留现有照片
  2. 更新表格并替换(或删除)现有图片
  3. 更新表格并添加图片(如果之前没有)
  4. 现在,方案2)和3)工作正常,但我正在努力的是方案1)。出于某种原因,任何现有图片(来自初始提交)都将被覆盖/删除。当我尝试调试时,我注意到我的if语句的else子句只是被跳过了。

    我怀疑这与我的视图或模型有关,因此我发布了我的控制器的相关代码。

        public ActionResult UpdateErrand(
            [Bind(Exclude                       = "Picture")]errands model)
        {
            // define variables for reuse
            var userID                          = User.Identity.GetUserId();
            DateTime nowUTC                     = DateTime.Now.ToUniversalTime();
            DateTime nowLocal                   = DateTime.Now.ToLocalTime();
            // get picture and get it to bytes
            string picture                      = Request.Form["editErrandCroppedPicture"];
            byte[] imageBytes                   = Convert.FromBase64String(picture);
            // define errand
            var errand                          = new errands
            {
                // basics
                UserID                          = userID,
                FirstName                       = UserManager.FindById(userID).FirstName,
                Email                           = UserManager.FindById(userID).Email,
                Phone                           = UserManager.FindById(userID).PhoneNumber,
                //Rating                        = 
    
                // form
                ID                              = model.ID,
                Category                        = model.Category,
                SubCategory                     = model.SubCategory,
                Title                           = model.Title,
                Description                     = model.Description,
                Location                        = model.Location,
                ASAP                            = model.ASAP,
                StartDateTime                   = model.StartDateTime,
                DurationInHours                 = model.DurationInHours,
                EndDateTime                     = model.EndDateTime,
                DateTimePosted                  = nowLocal,
                Currency                        = model.Currency,
                Offering                        = model.Offering,
                Price                           = model.Price,
                errandTax                       = model.errandTax,
                PaymentMethod                   = model.PaymentMethod,
                LatitudePosted                  = model.LatitudePosted,
                LongitudePosted                 = model.LongitudePosted,
                LocationPosted                  = model.LocationPosted,
                Published                       = true
            };
            // workaround to ensure picture is uploaded correctly
            if (imageBytes.Length               > 2)
            {
                errand.Picture                  = imageBytes;
            }
            else
            {
                errand.Picture                  = model.Picture;
            }
            // save errand to DB
            ERRANDOM.Entry(errand).State = EntityState.Modified;
            ERRANDOM.SaveChanges();
            // track user activity: post includes activity name, timestamp along with location, and if authenticated, user ID
            var SUCCESS                         = new UserActivities
            {
                UserID                          = userID,
                ActivityName                    = "EditErrand_Success",
                ActivityTimeStampUTC            = nowUTC,
                ActivityLatitude                = model.LatitudePosted,
                ActivityLongitude               = model.LongitudePosted,
                ActivityLocation                = model.LocationPosted
            };
            ERRANDOM.UserActivityList.Add(SUCCESS);
            ERRANDOM.SaveChanges();
            return RedirectToAction("errandPreview");
        }
    

2 个答案:

答案 0 :(得分:2)

如果将实体标记为已修改(.State = EntityState.Modified),则其所有映射的标量属性(即非导航属性)都将成为SQL更新语句的一部分。但是,可以从更新中排除选定的属性。以下是如何做到这一点:

...
LatitudePosted = model.LatitudePosted,
LongitudePosted = model.LongitudePosted,
LocationPosted = model.LocationPosted,
Published = true
errand.Picture = imageBytes;

ERRANDOM.Entry(errand).State = EntityState.Modified;

if (imageBytes.Length <= 2)
{
    ERRANDOM.Entry(errand).Property(e => e.Picture).IsModified = false;
}

ERRANDOM.SaveChanges();

如您所见,您现在可以无条件地分配errand.Picture = imageBytes:稍后当它不需要更新时将被排除。在没有图片更新的情况下(这可能是)最常见的情况下,这也节省了更新差事的带宽。

答案 1 :(得分:0)

您正在排除绑定图片,因此您的IF语句else子句将为“新”差事对象分配空值(model.Picture)。