vendredi 6 avril 2018

MVC controller if/else statement

this may be a stupid question, but I am just lost. I have a form that is submitted with data, then it goes into a preview view from where a user can decide whether to proceed posting or edit the form again.

Apart from submitting data, I also offer the possibility to upload a picture. All works fine, until I try to edit the form, where I basically have 3 scenarios:

a) update form and keep existing picture b) update form and replace (or delete) existing picture c) update form and add a picture (in case there was none before)

Now, scenario b) and c) work fine, but what I am struggling with is scenario a). For some reason any existing picture would be overridden/deleted and quite honestly, I cannot tell why. Even troubleshooting the behavior I notice that the else clause of my if statement is simply being skipped. So the if part is working as expected, but the else is just overlooked...

I doubt this has something to do with my view or the model, hence I am posting the relevant code of my controller. Please let me know if any further information is deemed helpful to solve that mystery. Thx!

    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");
    }

Aucun commentaire:

Enregistrer un commentaire