Sefer K
Sefer K

Reputation: 514

2 controller and 1 view in ASP.NET MVC

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?

Upvotes: 0

Views: 1039

Answers (3)

giorgian
giorgian

Reputation: 3825

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); %>

Upvotes: 0

Mark Seemann
Mark Seemann

Reputation: 233125

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.

Upvotes: 0

Arnis Lapsa
Arnis Lapsa

Reputation: 47567

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.

Upvotes: 4

Related Questions