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