its been 2days that i have a problem. in my model 'Colocataires' there is a property type of ApplicationUser 'Publisher'. thats mean the Publisher of a Colocation is the User him self. so the problem is the ModelState always is false, so the Creation never done. Model :
public class Colocataire
{
[Key]
public int ColocataireId { get; set; }
public string ApplicationUserID { get; set; }
public virtual ApplicationUser Publisher { get; set; }
[Required(ErrorMessage = "veuillez remplir ce champ !")]
public string Titre { get; set; }
.
.
.
[Required(ErrorMessage = "veuillez remplir ce champ !")]
public string Description { get; set; }
[DisplayName("Photo")]
public string PhotoColoc { get; set; }
}
Identity Model :
public class ApplicationUser : IdentityUser
{
[DisplayName("Nom"),Required]
public string LastName { get; set; }
[DisplayName("Prenom"), Required]
public string FirstName { get; set; }
[DisplayName("Sexe"), Required]
public string Gender { get; set; }
[DisplayName("Age"), Required]
public int Age { get; set; }
[DisplayName("Telephone"), Required]
public string Tel { get; set; }
public ICollection<OffreDeLocation> OffresLocation { get; set; }
public ICollection<Colocataire> Colocatires { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
HttpPost Create Action
[HttpPost]
[ValidateAntiForgeryToken]
[Route("Create")]
public ActionResult Create(Colocataire colocataire, HttpPostedFileBase upload)
{
ViewBag.currentUser = User.Identity.GetUserName();
if (ModelState.IsValid)
{
string path = Path.Combine(Server.MapPath("~/Uploads/Colocataires"), upload.FileName);
upload.SaveAs(path);
colocataire.PhotoColoc = upload.FileName;
colocataire.DateDajout = DateTime.UtcNow;
db.Colocataires.Add(colocataire);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(colocataire);
}
when i remove the ApplicationUser Property it works fine, so i dont know what im missing , pls help