提问人:Ahsan Shafique 提问时间:11/15/2023 最后编辑:Anna Andreeva RogotulkaAhsan Shafique 更新时间:11/17/2023 访问量:18
为什么 ModelState 在处理图像时有效 false
Why ModelState Isvalid false when working on images
问:
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(语言))] [必需(ErrorMessageResourceType = typeof(语言), ErrorMessageResourceName = “ImageIsRequied”)]
public string ImageUrlPath { get; set; }
[未映射]
public HttpPostedFileBase ImageFile { get; set; }
答:
0赞
Ahsan Shafique
11/15/2023
#1
var 错误 = ModelState.Values.SelectMany(v => v.Errors)。选择(e => e.ErrorMessage)。ToList();
使用它来检查哪些字段是必需的,并确保模型类也是必需属性
评论