简体   繁体   English

如何在视图页面中显示两个不同的列表?

[英]How to display two different list in view page?

I am New to ASP MVC ,I Having Two list in action method First list contains task details and second list contains Employee ids i want to send both list to the view page with paging.My Problem is i am not able to send both list to view 我是ASP MVC的新手,我在操作方法中有两个列表第一个列表包含任务详细信息,第二个列表包含员工ID,我想将两个列表都发送到带有分页的视图页面。我的问题是我无法将两个列表都发送到视图

public ActionResult DisplayProjectTask(string projectId, int? page)
{            
    List<Task> projectTasksList =bHelper.GetProjectTaskList(projectId);
    List<string> empIds = dbHelper.GetTaskEmployeesId(projectId);           
    return View(projectTasksList.ToPagedList(page ?? 1, 10));
}

Create a view model to hold them: 创建一个视图模型来保存它们:

public class FooViewModel
{
    public FooViewModel()
    {
       Tasks = new List<Task>();
       EmpIds = new List<string>();
    }

    public List<Task> Tasks { get; set; }
    public List<string> EmpIds { get; set; }
}

Populate them from your controller and return the model: 从您的控制器填充它们并返回模型:

public ActionResult DisplayProjectTask(string projectId, int? page)
{    
    var model = new FooViewModel();
    model.Tasks = dbHelper.GetProjectTaskList(projectId).ToPagedList(page ?? 1, 10);
    model.EmpIds = dbHelper.GetTaskEmployeesId(projectId);           
    return View(model);
}

Then add this to your view: 然后将此添加到您的视图:

@model FooViewModel

@foreach(var task in Model.Tasks)
{
   @* Replace with your properties in the Task object *@
   @task.ProperyName
}

@foreach(var empId in Model.EmpIds)
{
   @empId
}

You can create a Model for example: 您可以创建一个模型,例如:

public class MyTwoListsModel
{
    public List<Cars> ListName1;
    public List<Animals> ListName2;

    public MyTwoLists(List<Cars> list1, List<Animals> list2)
    {   
        this.ListName1 = list1;
        this.ListName2 = list2;
    }
}

And then create in a ViewController instance of it and the pass it to the View. 然后在其的ViewController实例中创建并将其传递给View。 If you have some question - ask 如果您有任何问题-问

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

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