当我向模型添加ApplicationUser属性时,创建ActionResult不起作用

时间:2018-08-11 19:06:28

标签: c# asp.net-mvc asp.net-identity

我有问题已经2天了。在我的模型'Colocataires'中,有一个ApplicationUser'Publisher'的属性类型。多数民众赞成在一个主机的发布者是用户他自己。因此问题在于ModelState始终为false,因此Creation从未完成。 型号:

    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; }

}

身份模型:

    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创建操作

        [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);
    }

当我删除ApplicationUser属性时,它可以正常工作,因此我不知道自己缺少什么,请帮忙

0 个答案:

没有答案