views:

100

answers:

3

I have 2 controllers, task and user.

I need to create a view for "create task to users". So I need "a user list" and "create task" view together.

Usually views inherit from only 1 class.

How can I create a view working with 2 classes?

Have any idea?

+3  A: 

Your view can't and should not inherit from 2 classes.

Create a view model for your user which has a list of task view models.

Arnis L.
thanks, this is a good solution
Sefer KILIÇ
my view model now as below;public class CreateTaskViewModel { public CreateTaskViewModel(List<MU_User> user, MU_Task task) { this.Users = user; this.Task = task; } public List<MU_User> Users{ get; private set; } public MU_Task Task { get; private set; } }
Sefer KILIÇ
A: 

You can implement each View (user list and create task) as partial views (.ascx user controls), and call each of these from within your View (.aspx page).

Rendering a partial view is done by using the RenderPartial method:

this.Html.RenderPartial("UserList");
this.Html.RenderPartial("CreateTask");

This will allow you to reuse and combine Views accross different Views (pages).

As AmisL points out, you can pass different parts of your ViewModel to each partial view.

Mark Seemann
And it's good to remember that if partial view receives null as view model, it tries to pick up view model from view. That can cause a data type mismatch error.
Arnis L.
A: 

A View can receive any class as Model, so just write a custom class, like:

public class UserListAndTasks
{
    public IQuerable<User> UserList;
    public Task Task;
}

Then, in your View, you can use partials:

<%= Html.RenderPartial("UserList", Model.UserList); %>
<%= Html.RenderPartial("CreateTask", Model.Task); %>
giorgian