Justin Adkins
Justin Adkins

Reputation: 1234

MVC view with two different models

I have a view called AccountManager on this view I have a section to update your profile information and update your password.

Both functions require a different model. ManageUserViewModel and ChangePasswordViewModel.

On the AccountManager view, both sections are rendered via @Html.Partial.

When I try to display the page, I receive the following error: "The model item passed into the dictionary is of type 'WebUI.Models.ManageUserViewModel', but this dictionary requires a model item of type 'WebUI.Models.ChangePasswordViewModel'."

How can I render both views without receiving this error?

Upvotes: 0

Views: 111

Answers (1)

Guillermo Oramas R.
Guillermo Oramas R.

Reputation: 1303

ViewModel:

public class AccountManagerViewModel
{ 
    public ManageUserViewModel manageUserViewModel { get; set; }
    public ChangePasswordViewModel changePasswordViewModel { get; set; }
}

The View:

@model AccountManagerViewModel
/*HTML CODE*/
@Html.RenderPartial("_PartialChangePassword", Model.changePasswordViewModel)
@Html.RenderPartial("_PartialManageUser", Model.manageUserViewModel)

So you just play with the viewModels. Remember that they can have anything you need in the view. Let me know.

Upvotes: 2

Related Questions