0

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 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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; } }
0

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.

1 Comment

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.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.