0

I have a trry/catch with addModelError. But it is now showing. And yes, I have put a breakpoint on that line and it reached the line, but now showing.

here is the actioni metthod:

[HttpPost]        
        public ActionResult EditPhotos(ImageUploadModel ImageUpload)
        {
            var validImageTypes = new string[]
            {
        "image/gif",
        "image/jpeg",
        "image/pjpeg",
        "image/png"
            };
            try
            {

                if (ImageUpload.file == null && ImageUpload.file.ContentLength == 0)
                {
                    ModelState.AddModelError("file", "This field is required");

                }


                else if (!validImageTypes.Contains(ImageUpload.file.FileName.Substring(ImageUpload.file.FileName.LastIndexOf('.'))))
                {
                   ModelState.AddModelError("file", "Please upload Your Photo of type: " + string.Join(", ", validImageTypes));
                   return View(ImageUpload);                   
                }

                else if (ImageUpload.file != null && ImageUpload.file.ContentLength > 0)
                {
                    var DirSeparator = Path.DirectorySeparatorChar;
                    var fileName = Path.GetFileName(ImageUpload.file.FileName);
                    //var fileName2 = Path.GetFileName(@"\\Cyclist3.jpg");
                    var path = Server.MapPath(@"\\Images\\profile" + DirSeparator + fileName.Replace('+', '_'));
                    ImageUpload.file.SaveAs(path);
                    ViewBag.Message = "File has been uploaded successfully";
                    ModelState.Clear();
                }


            }
            catch (FileLoadException ex)
            {

                throw;
            }



            if (ModelState.IsValid)
            {
                string username = User.Identity.Name;
                // Get the userprofile
                UserProfile user = db.userProfiles.FirstOrDefault(u => u.UserName.Equals(username));

                // Update fields

                byte[] uploadedFile = new byte[ImageUpload.file.ContentLength];              
                ImageUpload.file.InputStream.Read(uploadedFile, 0, ImageUpload.file.ContentLength);
                user.file = uploadedFile;
                user.ImageMimeType = ImageUpload.file.ContentType;

                db.Entry(user).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                            eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    throw;
                }

            }

            return RedirectToAction("Edit", routeValues: new { controller = "Account", activetab = "tabs-2" });
        }

and this is the view:

 <div class="lifile">
                    <div id="upload-choices">
                        <div class="editor-label">


                            @Html.ValidationMessageFor(model => model.file)

                        </div>

                    </div>
                  </div>

so for exmaple this:

{
                       ModelState.AddModelError("file", "Please upload Your Photo of type: " + string.Join(", ", validImageTypes));
                       return View(ImageUpload);                   
                    }

I put a breakpoint on the AddModelError and it reach that line, but the error is not showing.

Thank you

I tried this:

View:

<div class="lifile">
                    <div id="upload-choices">
                        <div class="editor-label">


                            @Html.ValidationMessageFor(model => model.file)
                           @Html.ValidationSummary(true)

                        </div>

                    </div>
                  </div>

Action method

 else if (!validImageTypes.Contains(ImageUpload.file.ContentType))
                {
                    ModelState.AddModelError(string.Empty, "Please choose either a GIF, JPG or PNG image.");
                    return View(ImageUpload);

                }
svick
  • 236,525
  • 50
  • 385
  • 514
Niels Savant
  • 315
  • 4
  • 6
  • 16

1 Answers1

0

I believe the ModelStateDictionary will not tag along on your RedirectResult, you should return a ViewResult.

If you put a breakpoint on the AddModelError line, that line will not have executed yet. Put a breakpoint on the next line, and the Error should be there.

severin
  • 5,203
  • 9
  • 35
  • 48
  • Thank you for your comment, but I only have a viewmodel: ImageUploadModel ImageUpload but if I return that: view(ImageUpload) doesnt work – Niels Savant Oct 30 '14 at 09:41