C# 将ApplicationUser属性添加到模型时,Create ActionResult不起作用
我有问题已经20天了。在我的模型“Colocataires”中,有一个属性类型为ApplicationUser“Publisher”。这意味着一个托管的发布者就是他自己的用户。所以问题是ModelState始终为false,所以创建从未完成。 型号: 身份模型:C# 将ApplicationUser属性添加到模型时,Create ActionResult不起作用,c#,asp.net-mvc,asp.net-identity,C#,Asp.net Mvc,Asp.net Identity,我有问题已经20天了。在我的模型“Colocataires”中,有一个属性类型为ApplicationUser“Publisher”。这意味着一个托管的发布者就是他自己的用户。所以问题是ModelState始终为false,所以创建从未完成。 型号: 身份模型: public class ApplicationUser : IdentityUser { [DisplayName("Nom"),Required] public string LastName { get;
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;
}
}
当我删除ApplicationUser属性时,它工作正常,因此我不知道我缺少了什么,请帮助缺少一个必填字段。在“ModelState.IsValid”上放置一个断点,然后深入研究its键。其中一个选项具有键和值。该值将告诉您错误是什么。请参见:
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]
[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);
}