简体   繁体   English

在asp.net mvc中的父视图中有多个部分/子视图

[英]multiple partial/child views in parent view in asp.net mvc

I want to create a view page like below sketch. 我想创建一个如下图所示的视图页面。 It has 3 sections . 它分为三个部分。

Section 1: Table List - showing the list of available products 第1节:表格列表-显示可用产品列表

Section 2: Summary 1 - showing the 2 of newest launched products with short descriptions 第2节:摘要1-用简短的说明显示最新推出的2种产品

Section 3: Summary 2 - showing the 3 of newest group discussion topics with short descriptions 第3节:摘要2-用简短说明显示最新的小组讨论主题中的3个

在此处输入图片说明

For that I just followed this Tutorial 为此,我只是遵循了本教程

I just tried to create show the table as partial view initially.later I hope to add other two sections. 我只是尝试最初将表显示为部分视图。后来我希望添加其他两个部分。

Designed model like this 像这样设计的模型

public class DashboardViewModel
{
    public ProductTableViewModel ProductTable;

    public NewProductViewModel NewProduct;

    public DiscussionsViewModel Discussions;
}
public class ProductTableViewModel
{
    [Display(Name = "S.No")]
    public string Product_ID { get; set; }

    [Display(Name = "Product Title")]
    public string Product_Title_EN { get; set; }

    .....
}

for this I just created DAL like this 为此,我像这样创建了DAL

  public class DashboardData
    {
        public DBEntities db = new DBEntities();
        public ProductTableViewModel GetAllProducts()
        {
              var result = (from product in db.AB_Product
                            .....
                            select new ProductTableViewModel
                            {
                            .....     
                            }).ToList();

            return result;
        }

then I call it in controller like this . 然后我在控制器中这样称呼它。

    public ActionResult Dashboard()
    {

        ProductTableViewModel pdct = new ProductTableViewModel();
        DashboardViewModel puff = new DashboardViewModel();


        puff.ProductTable = pdct.GetAllProducts();

        return View(puff);    
    }

then I'm getting following errors 然后我得到以下错误

Cannot implicitly convert type System.Collections.Generic.List<project_name.Models.ProductTableViewModel>' to 'project_name.Models.ProductTableViewModel' 无法将类型System.Collections.Generic.List <project_name.Models.ProductTableViewModel>'隐式转换为'project_name.Models.ProductTableViewModel'

other error 其他错误

'ProductTableViewModel' does not contain a definition for 'GetAllProducts' and no extension method 'GetAllProducts' accepting a first argument of type 'ProductTableViewModel' “ ProductTableViewModel”不包含“ GetAllProducts”的定义,也没有扩展方法“ GetAllProducts”接受“ ProductTableViewModel”类型的第一个参数

Change type of pdct ProductTableViewModel to DashboardData 将pdct ProductTableViewModel的类型更改为DashboardData

DashboardData pdct = new DashboardData()

and

public class DashboardViewModel
{
    public List<ProductTableViewModel>  ProductTable;

    public NewProductViewModel NewProduct;

    public DiscussionsViewModel Discussions;
}
public List<ProductTableViewModel> GetAllProducts()
        {
              var result = (from product in db.AB_Product
                            .....
                            select new ProductTableViewModel
                            {
                            .....     
                            }).ToList();

            return result;
        }

Here you are trying to return a List of ProductTableViewModels not a single item of ProductTableViewModel .So for first change the return type to List<ProductTableViewModel> 在这里,你试图返回ProductTableViewModels的列表中ProductTableViewModel。所以单个项目的第一个变化返回类型List<ProductTableViewModel>

 public class DashboardData
 {
         public DBEntities db = new DBEntities();
         public List<ProductTableViewModel> GetAllProducts()
         {
               var result = (from product in db.AB_Product
                                .....
                             select new ProductTableViewModel
                             {
                             .....     
                             }).ToList();

               return result;
         }
 }

and then try to change your ViewModel as you want 然后尝试根据需要更改ViewModel

public class DashboardViewModel
{
    public List<ProductTableViewModel> ProductTable;

    public NewProductViewModel NewProduct;

    public DiscussionsViewModel Discussions;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过部分ASP.NET MVC 4视图在父列表内创建子对象 - How to create child objects inside parent list through partial ASP.NET MVC 4 views 如何在一个视图中使用其视图模型调用多个局部视图 - ASP.NET MVC - How to call multiple partial views with their View Models inside a view - ASP.NET MVC 强类型视图(ASP.NET MVC5)中的多个局部视图 无法获取相对路径 - Multiple partial views in a strongly typed view (ASP.NET MVC5) ? Unable to get relative Path In View 部分视图中的ASP.NET MVC验证并返回到父视图 - ASP.NET MVC Validation in Partial View and return to Parent view 显示具有部分视图的多个表。 Asp.Net Mvc - Showing multiple tables with partial views. Asp.Net Mvc ASP.NET MVC呈现不必要的多个局部视图 - ASP.NET MVC rendering unwanted multiple partial views 刷新 ASP.NET MVC 中的多个局部视图 - Refreshing multiple partial views in ASP.NET MVC 在asp.net MVC的一个页面上有多个帖子,具有部分视图 - Multiple posts on a single page asp.net MVC with partial views 当两个部分视图都包含在 ASP.NET MVC 中的同一父视图中时,如何将数据列表从一个部分视图传递到另一个部分视图? - How to pass a list of data from one partial view to another partial view, when both partial views are contained in a same parent view in ASP.NET MVC? 在ASP.net MVC 4中使用部分视图 - Using partial views in ASP.net MVC 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM