下载MVC中的列表

时间:2017-02-08 17:16:27

标签: c# asp.net-mvc entity-framework dependency-injection html.dropdownlistfor

在我的MVC项目(代码优先方法)中,我为每个用户创建了侧导航菜单的模型类。我需要在相关视图中创建一个下拉列表,其中包含AspNetUsers表的不同用户名(自定义字段)。目标是显示NavigationMenus表中与相关用户关联的每个语音并创建一个新语音,但我不明白如何制作: 1)下拉列表 2)在选择所需用户后刷新页面,以显示相关记录。

型号:

public class NavigationMenu
{
    public NavigationMenu()
    {
        MenuChildren = new HashSet<NavigationMenu>();
    }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }      
    public string Text { get; set; }
    public string Action { get; set; }
    public string Controller { get; set; }
    public string Icon { get; set; }
    public bool Selected { get; set; }

    public int? NavigationMenuId { get; set; }

    public virtual ICollection<NavigationMenu> MenuChildren { get; set; }
    public virtual NavigationMenu NavigationMenus2 { get; set; }

    //one-to-many relationships AspNetUsers -> NavigationMenu
    public string ApplicationUserId { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }

}

上下文:

public class NavigationMenuContext : DbContext
{

    public NavigationMenuContext() : base("name=DefaultConnection")  // connectionstring name define in your web.config
    {
        this.Configuration.LazyLoadingEnabled = false;
    }

    public DbSet<NavigationMenu> NavigationMenus { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        // Configure the primary Key for the NavigationMenu 
        modelBuilder.Entity<NavigationMenu>()
            .HasKey(t => t.Id);            

        modelBuilder.Entity<NavigationMenu>()
            .HasOptional(c => c.NavigationMenus2)
            .WithMany(c => c.MenuChildren)
            .HasForeignKey(c => c.NavigationMenuId);


        modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
        modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });

    }

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

控制器:

public class NavigationController : Controller
{
    NavigationMenuContext navigationMenuContext;
    public NavigationController()
    {
        navigationMenuContext = new NavigationMenuContext();
    }


    [HttpGet]
    [Authorize(Roles = "Admin")]
    public ActionResult List()
    {            
        ViewBag.Name = new SelectList(navigationMenuContext.NavigationMenus.Select(m => m.ApplicationUser).Distinct().ToList());                             
        return View();
    }                  

}

在视图List.cshtml中:

@model IEnumerable<siapp.Models.NavigationMenu>

<div class="form-group">
        @Html.Label("Role", new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.DropDownList("User", (SelectList)ViewBag.Name, " ", new { @class = "form-control" })                
        </div>
    </div>

我有这个错误:

{&#34;对象&#39; dbo.ApplicationUsers&#39;无效。&#34;}

更新:

我会尝试另一种方式:

var context = new IdentityDbContext();
var allUsers = context.Users.Select(m => m.UserName).Distinct().ToList();
ViewBag.Name = new SelectList(allUsers);
Return View();

有一种通过API查询ApplicationUser以避免数据库依赖的方法吗?

0 个答案:

没有答案