2

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

dr1sshamra
  • 81
  • 2
  • 9
  • One of your required fields is missing. Put a breakpoint on "ModelState.IsValid" then dig into the its Keys. One of the options has the Key and the value. That, value will tell you what the error is. – alikuli Aug 11 '18 at 20:03
  • see this: https://stackoverflow.com/questions/1352948/how-to-get-all-errors-from-asp-net-mvc-modelstate – alikuli Aug 11 '18 at 20:06

0 Answers0