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; }
型
1条答案
按热度按时间hzbexzde1#
var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
用它来检查哪些字段是必需的,并确保你的模型类也是必需的属性