gpt4 book ai didi

c# - 在 Asp.net Mvc 5 应用程序中将用户分配给项目列表

转载 作者:太空宇宙 更新时间:2023-11-03 15:47:11 25 4
gpt4 key购买 nike

我想为我的项目分配用户但是当我运行它时出现问题我得到错误

不支持每种类型的多个对象集。对象集“ApplicationUsers”和“Users”都可以包含“ProjectManagementSystem.Models.ApplicationUser”类型的实例。这是在我的 IdentityModels.cs 中

public class ApplicationUser
: IdentityUser<string, ApplicationUserLogin,
ApplicationUserRole, ApplicationUserClaim>
{
public ApplicationUser()
{
this.Id = Guid.NewGuid().ToString();

}


public async Task<ClaimsIdentity>
GenerateUserIdentityAsync(ApplicationUserManager manager)
{
var userIdentity = await manager
.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
public string Name { get; set; }

public string LastName { get; set; }

public string User_name { get; set; }

public string City { get; set; }

public virtual ICollection<Project> Projects { get; set; }
}

这也是来自IdentityModels.cs

modelBuilder.Entity<Project>()
.HasMany<ApplicationUser>(c => c.UsersOnProjects).WithMany(i => i.Projects)
.Map(t => t.MapLeftKey("ProjectID")
.MapRightKey("Id")
.ToTable("Projects"));

这就是:

    public System.Data.Entity.DbSet<ProjectManagementSystem.Models.Clients> Clients { get; set; }

public System.Data.Entity.DbSet<ProjectManagementSystem.Models.ProjectCategory>
ProjectCategories { get; set; }

public System.Data.Entity.DbSet<ProjectManagementSystem.Models.Project> Projects
{ get; set; }

public System.Data.Entity.DbSet<ProjectManagementSystem.Models.ApplicationUser>
ApplicationUsers { get; set; }

现在这是我的项目模型

  public class Project
{
[Key]
public int ProjectId { get; set; }

[Required]
[Display(Name = "Project Name")]
public string ProjectTitle { get; set; }

[Required]
public string Description { get; set; }

public string Url { get; set; }

public int ProjectCategoryId { get; set; }

[Required]
[Display(Name = "Project Category")]
public virtual ProjectCategory ProjectCategory { get; set; }

public int ClientId { get; set; }

public virtual Clients Client { get; set; }

public string Budget { get; set; }

[Display(Name = "Start Date")]
public string StartDate { get; set; }

[Display(Name = "End Date")]
public string EndDate { get; set; }

public string UserId { get; set; }

public virtual ApplicationUser User { get; set; }

public virtual ICollection<ApplicationUser> UsersOnProjects { get; set; }
}

我尝试了一切,比如删除这部分

 public System.Data.Entity.DbSet<ProjectManagementSystem.Models.ApplicationUser> 
ApplicationUsers { get; set; }

更改我的项目 Controller

  ViewBag.UserId = new SelectList(db.ApplicationUsers, "Id", "Name");

To

ViewBag.UserId = new SelectList(db.Users, "Id", "Name");

还有更多,但我总是得到同样的错误....我已经从此链接创建了用户角色和组 http://typecastexception.com/post/2014/08/10/ASPNET-Identity-20-Implementing-Group-Based-Permissions-Management.aspx

也许这让我出错了。我正在使用 entityframwork 创建我的 Controller

最佳答案

我已经通过将这一行放在我的 View 中解决了这个问题

@Html.ValidationSummary(false)

然后将 var modelStateErrors 和断点放在 if (ModelState.IsValid) 上

var modelStateErrors = this.ModelState.Values.SelectMany(m => m.Errors);
if (ModelState.IsValid)
{
db.Projects.Add(project);
db.SaveChanges();
return RedirectToAction("Index");
}

之后我发现我的项目模型中有错误消息,并且需要 ProjectCategory。我删除了它,现在它确实可以工作了....

public int ProjectCategoryId { get; set; }

[Display(Name = "Project Category")]
public virtual ProjectCategory ProjectCategory { get; set; }

关于c# - 在 Asp.net Mvc 5 应用程序中将用户分配给项目列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27649044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com