C# 如何在MVC中将多个对象从控制器动作传递到视图?
如何在不使用ViewData和ViewBag的情况下传递多个对象进行查看 示例:C# 如何在MVC中将多个对象从控制器动作传递到视图?,c#,asp.net-mvc,C#,Asp.net Mvc,如何在不使用ViewData和ViewBag的情况下传递多个对象进行查看 示例: [AllowAnonymous] public ActionResult RandomView() { // Customer. var customers = _context.Customers.ToList(); List<CustomerDto> customersDto = new List<CustomerDto>(); foreach (var cus
[AllowAnonymous]
public ActionResult RandomView()
{
// Customer.
var customers = _context.Customers.ToList();
List<CustomerDto> customersDto = new List<CustomerDto>();
foreach (var customer in customers)
{
customersDto.Add(new CustomerDto() {
Id = customer.Id,
Name = customer.Name
});
}
// Reports.
var Reports= _context.Reports.ToList();
List<ReportsDto> reportsDto= new List<ReportsDto>();
foreach (var report in reports)
{
reportsDto.Add(new ReportsDto() {
Id = report.Id,
Name = report.Name
});
}
return View(); // How to Pass Both CustomerDto and reportDto ?
}
[AllowAnonymous]
公共行动结果随机视图()
{
//顾客。
var customers=_context.customers.ToList();
List customersDto=新列表();
foreach(客户中的var客户)
{
添加(新的CustomerDto(){
Id=customer.Id,
Name=customer.Name
});
}
//报告。
var Reports=_context.Reports.ToList();
List reportsDto=新列表();
foreach(报告中的var报告)
{
reportsDto.Add(新的reportsDto(){
Id=report.Id,
Name=report.Name
});
}
return View();//如何同时传递CustomerDto和reportDto?
}
我想将
CustomerDto
和reportDto作为强类型传递给视图。可能吗?
您应该将这两种类型包装到某个ViewModel类中,如下所示:
public class CustomerData
{
public List<CustomerDto> Customer { get; set; }
public List<ReportsDto> Reports { get; set; }
}
您应该将这两种类型包装到某个ViewModel类中,如下所示:
public class CustomerData
{
public List<CustomerDto> Customer { get; set; }
public List<ReportsDto> Reports { get; set; }
}
为视图提供模型类。在该视图中,可以引用模型类。创建模型类的两个列表属性。见下面的示例:
public class RandomViewModel
{
public List<CustomerDto> Customers { get; set; }
public List<ReportDto> Reports { get; set; }
}
[AllowAnonymous]
public ActionResult RandomView()
{
// Customer.
var customers = _context.Customers.ToList();
List<CustomerDto> customersDto = new List<CustomerDto>();
foreach (var customer in customers)
{
customersDto.Add(new CustomerDto() {
Id = customer.Id,
Name = customer.Name
});
}
// Reports.
var Reports= _context.Reports.ToList();
List<ReportsDto> reportsDto= new List<ReportsDto>();
foreach (var report in reports)
{
reportsDto.Add(new ReportsDto() {
Id = report.Id,
Name = report.Name
});
}
var randomViewModel = new RandomViewModel() {
Customers = customersDto,
Reports = reportsDto
};
return View(randomViewModel);
}
公共类随机视图模型
{
公共列表客户{get;set;}
公共列表报告{get;set;}
}
[异名]
公共行动结果随机视图()
{
//顾客。
var customers=_context.customers.ToList();
List customersDto=新列表();
foreach(客户中的var客户)
{
添加(新的CustomerDto(){
Id=customer.Id,
Name=customer.Name
});
}
//报告。
var Reports=_context.Reports.ToList();
List reportsDto=新列表();
foreach(报告中的var报告)
{
reportsDto.Add(新的reportsDto(){
Id=report.Id,
Name=report.Name
});
}
var randomViewModel=新的randomViewModel(){
Customers=customersDto,
Reports=reportsDto
};
返回视图(随机视图模型);
}
为您的视图提供一个模型类。在该视图中,可以引用模型类。创建模型类的两个列表属性。见下面的示例:
public class RandomViewModel
{
public List<CustomerDto> Customers { get; set; }
public List<ReportDto> Reports { get; set; }
}
[AllowAnonymous]
public ActionResult RandomView()
{
// Customer.
var customers = _context.Customers.ToList();
List<CustomerDto> customersDto = new List<CustomerDto>();
foreach (var customer in customers)
{
customersDto.Add(new CustomerDto() {
Id = customer.Id,
Name = customer.Name
});
}
// Reports.
var Reports= _context.Reports.ToList();
List<ReportsDto> reportsDto= new List<ReportsDto>();
foreach (var report in reports)
{
reportsDto.Add(new ReportsDto() {
Id = report.Id,
Name = report.Name
});
}
var randomViewModel = new RandomViewModel() {
Customers = customersDto,
Reports = reportsDto
};
return View(randomViewModel);
}
公共类随机视图模型
{
公共列表客户{get;set;}
公共列表报告{get;set;}
}
[异名]
公共行动结果随机视图()
{
//顾客。
var customers=_context.customers.ToList();
List customersDto=新列表();
foreach(客户中的var客户)
{
添加(新的CustomerDto(){
Id=customer.Id,
Name=customer.Name
});
}
//报告。
var Reports=_context.Reports.ToList();
List reportsDto=新列表();
foreach(报告中的var报告)
{
reportsDto.Add(新的reportsDto(){
Id=report.Id,
Name=report.Name
});
}
var randomViewModel=新的randomViewModel(){
Customers=customersDto,
Reports=reportsDto
};
返回视图(随机视图模型);
}
方式1:
[AllowAnonymous]
public ActionResult RandomView()
{
// Customer.
var customers = _context.Customers.ToList();
List<CustomerDto> customersDto = new List<CustomerDto>();
foreach (var customer in customers)
{
customersDto.Add(new CustomerDto() {
Id = customer.Id,
Name = customer.Name
});
}
// Reports.
var Reports= _context.Reports.ToList();
List<ReportsDto> reportsDto= new List<ReportsDto>();
foreach (var report in reports)
{
reportsDto.Add(new ReportsDto() {
Id = report.Id,
Name = report.Name
});
}
return View(); // How to Pass Both CustomerDto and reportDto ?
}
创建同时具有两个属性的视图模型&将该视图模型传递给视图
public class ViewModel
{
public List<CustomerDto> Customers {get;set;}
public List<ReportsDto> Reports {get;set;}
}
方式1:
[AllowAnonymous]
public ActionResult RandomView()
{
// Customer.
var customers = _context.Customers.ToList();
List<CustomerDto> customersDto = new List<CustomerDto>();
foreach (var customer in customers)
{
customersDto.Add(new CustomerDto() {
Id = customer.Id,
Name = customer.Name
});
}
// Reports.
var Reports= _context.Reports.ToList();
List<ReportsDto> reportsDto= new List<ReportsDto>();
foreach (var report in reports)
{
reportsDto.Add(new ReportsDto() {
Id = report.Id,
Name = report.Name
});
}
return View(); // How to Pass Both CustomerDto and reportDto ?
}
创建同时具有两个属性的视图模型&将该视图模型传递给视图
public class ViewModel
{
public List<CustomerDto> Customers {get;set;}
public List<ReportsDto> Reports {get;set;}
}
为什么要否决这个?这就是MVC中返回结果的方式-创建一个包含所有必需数据的ViewModel。和其他答案一样,为什么要否决这个?这就是MVC中返回结果的方式-创建一个包含所有必需数据的ViewModel。这和其他答案也一样