Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在asp.net mvc 4中使用viewmodel创建单视图?_Asp.net_Asp.net Mvc 4_Viewmodel - Fatal编程技术网

如何在asp.net mvc 4中使用viewmodel创建单视图?

如何在asp.net mvc 4中使用viewmodel创建单视图?,asp.net,asp.net-mvc-4,viewmodel,Asp.net,Asp.net Mvc 4,Viewmodel,我有不同型号的图片、页面和页面类别 public class Image { public int ImageId { get; set; } public string ImageTitle { get; set; } public string ImageURL { get; set; } } public class Page { public int PageId { get; set; }

我有不同型号的图片、页面和页面类别

public class Image
    {
        public int ImageId { get; set; }
        public string ImageTitle { get; set; }
        public string ImageURL { get; set; }
    }



public class Page
    {
        public int PageId { get; set; }
        public string PageTitle { get; set; }
        public string Content { get; set; }
        public int PageCategoryId { get; set; }
        public virtual PageCategory PageCategory { get; set; }
    }



public class PageCategory
    {
        public int PageCategoryId { get; set; }
        public string CategoryName { get; set; 
        public virtual ICollection<Page> Pages { get; set; }
    }
公共类映像
{
公共int-ImageId{get;set;}
公共字符串ImageTitle{get;set;}
公共字符串ImageURL{get;set;}
}
公共类页面
{
public int PageId{get;set;}
公共字符串PageTitle{get;set;}
公共字符串内容{get;set;}
public int PageCategoryId{get;set;}
公共虚拟页面类别页面类别{get;set;}
}
公共类页面类别
{
public int PageCategoryId{get;set;}
公共字符串CategoryName{get;set;
公共虚拟ICollection页{get;set;}
}
DBContext类是

 class DemoContext:DbContext
    {
        public DbSet<PageCategory> PageCategories { get; set; }
        public DbSet<Page> Pages { get; set; }
        public DbSet<Image> Images { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        }


    }
类DemoContext:DbContext
{
公共数据库集页面类别{get;set;}
公共数据库集页面{get;set;}
公共数据库集映像{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();
}
}
我想知道如何使用ViewModel将所有模型数据放到主页上

例如:
如何从多个型号中获取主页中的图像列表和页面列表?

您可能需要以下内容:

视图模型:

    //Create a viewModel with all the properties that you need
    public class ViewModel
        {
            public int ImageId { get; set; }
            public string ImageTitle { get; set; }
            public string ImageURL { get; set; }
            public int PageId { get; set; }
            public string PageTitle { get; set; }
            public string Content { get; set; }
            public int PageCategoryId { get; set; }
            public string CategoryName { get; set; }
        }
控制器:

使用(DemoContext db=new DemoContext()){
List viewData=(从数据库页中的p开始)
从p.PageCategoryId上的db.PageCategory加入pc等于pc.PageCategoryId
选择新建ViewModel(){
PageTitle=p.PageTitle,
CategoryName=pc.CategoryName
//…设置所需的所有属性
})
}
返回视图(viewData);
注意:我没有将图像添加到查询中,因为没有显式关系
在Image和others表之间,所以我允许您这样做。

创建另一个类,并在其中定义上述三个类。如下所示

public class MyView
{
    public List<Image> Images { get; set; }
    public List<Page> Pages { get; set; }
    public List<PageCategory> PageCategories { get; set; }
}
最后我得到了答案:

公共类ViewModelDemo {


看看这个,创建一个包含属性
IEnumerable
IEnumerable
的视图模型,在控制器中填充它并将它传递给视图。@StephenMuecke如何在视图模型中的IEnumerable之后填充控制器中的数据?
public class MyView
{
    public List<Image> Images { get; set; }
    public List<Page> Pages { get; set; }
    public List<PageCategory> PageCategories { get; set; }
}
public ActionResult Index()
{
    MyView myView = // Get it using your logic
    return View(myView);
}
    public IEnumerable<Image> images { get; set; }
    public IEnumerable<Pages> pages { get; set; }
    public IEnumerable<PageCategory> pagecategories { get; set; }

}
    private DemoContext db=new DemoContext();
    public ActionResult Index()
    {
    ViewModelDemo vm = new ViewModelDemo();

    vm.images = db.Images.ToList();
    vm.pages=db.Pagess.ToList();
    vm.pagecategories=db.PageCategories.ToList();

    return View(vm);
    }