简体   繁体   中英

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

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. If you have some question - ask

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