[英]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.