简体   繁体   中英

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

Section 2: Summary 1 - showing the 2 of newest launched products with short descriptions

Section 3: Summary 2 - showing the 3 of newest group discussion topics with short descriptions

在此处输入图片说明

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

  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'

other error

'ProductTableViewModel' does not contain a definition for 'GetAllProducts' and no extension method 'GetAllProducts' accepting a first argument of type 'ProductTableViewModel'

Change type of pdct ProductTableViewModel to 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>

 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

public class DashboardViewModel
{
    public List<ProductTableViewModel> ProductTable;

    public NewProductViewModel NewProduct;

    public DiscussionsViewModel Discussions;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM