asp.net 为什么在处理图像时ModelState Isvalid为false

afdcj2ne  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(119)
public ActionResult Signup(UserTable u)
 {
     string fileName = Path.GetFileNameWithoutExtension(u.ImageFile.FileName);
     string extension = Path.GetExtension(u.ImageFile.FileName);
     fileName = fileName + extension;
     u.ImageUrlPath = "/Images/" + fileName;
     fileName = Path.Combine(Server.MapPath("/Images/"), fileName);
     u.ImageFile.SaveAs(fileName);

     if (ModelState.IsValid)
     {
        PasswordEncrpt EncrptedData = new PasswordEncrpt();

         u.EmailVerification = false;
         u.ActiveCode = Guid.NewGuid();
         //u.EncryptedPassword = "fff";
         u.EncryptedPassword = EncrptedData.Encode(u.SimplePassword);
         //u.LastLogin = DateTime.Now;
         u.Active = true;
         u.RegisterDate = DateTime.Now;

         Db.UserTables.Add(u);
         int a = Db.SaveChanges();
         if (a > 0)
         {
             SendMailToUser(u.Email, u.ActiveCode.ToString());

             return Json(new { success = true });
         }
     }
     else
     {
         //return Json(false);
         //return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "An error occurred");

         //var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
         //return Json(new { success = false, errors });

         var errors = ModelState.ToDictionary(model => model.Key , model => model.Value.Errors.Select(e => e.ErrorMessage).ToList());

         return Json(new { success = false, errors });
     }
     return Json(false);
 }

字符串
请在asp.net mvc ef中回答此问题,使用datebase中的存储路径处理图像,但图像通过
[Display(Name =“ImageUrlPath”,ResourceType = typeof(Language))] [Required(ErrorMessageResourceType = typeof(Language),ErrorMessageResourceName =“ImageIsRequied”)]

public string ImageUrlPath {  get; set; }


[未Map]

public HttpPostedFileBase ImageFile { get; set; }

hzbexzde

hzbexzde1#

var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
用它来检查哪些字段是必需的,并确保你的模型类也是必需的属性

相关问题