2

When using VS2013 and creating a new web project (MVC5 / .net 4.5 / EF6) the default project template comes with a standard AccountController that implements the action "Manage". From what I can tell, there's no viewmodel explicitly passed to the Manage view (indeed, the view itself doesn't have a strongly typed model).

The thing is; inside the Manage view is the following line;

@Html.Partial("_ChangePasswordPartial")

When looking at that particular view, it shows a strongly typed viewmodel as;

@model MyProject.Models.ManageUserViewModel

Yet nowhere in the project can I find this viewmodel being populated or sent to that partial view. Can someone tell me WHERE the view is getting this viewmodel from (and where the viewmodel gets constructed)?

The reason is; I am trying to modify the "Manage" view to take my own viewmodel, and then want to pass a property of that viewmodel to the "_ChangePasswordPartial", so it looks something like this:

public ActionResult Manage()
{
    CustomViewModel viewModel = bLogic.CreateViewModel();
    viewModel.ChangePasswordViewModel = bLogic.CreatePasswordViewModel();
    return View(viewModel);
}

And then in my "Manage" view, have:

@Html.Partial("_ChangePasswordPartial", Model.ChangePasswordViewModel)
user1861826
  • 157
  • 1
  • 2
  • 6

1 Answers1

3

I think your issue is in understanding what the model is being used for.

"Yet nowhere in the project can I find this viewmodel being populated or sent to that partial view"

There is no need to populate the _ChangePasswordPartial view model since there is no data being used in the partial view initially (GET).

The view model is used after the form is submitted (POST). It gets created/populated in AccountController.Manage(ManageUserViewModel model) from the data submitted by the user, this is automatically done via Model Binders (you can create custom ones as well).

Within the Manage(ManageUserViewModel model) action the model is then validated and ultimately used to change/add a password to the user if the model is valid. If the model state is invalid the model gets returned to the Manage view, along with the model state which is used to display validation errors:

// If we got this far, something failed, redisplay form
return View(model);

This returns the Manage view, which in turn calls the _ChangePasswordPartial view, which inherently now has a populated ManageUserViewModel model.

Now why is there no model explicitly passed to the Partial from the Manage view?

Quite simply the partial view is inheriting the model of the parent view: In MVC do partial views inherit the models of their Parent views?

Community
  • 1
  • 1
Denny Ferrassoli
  • 1,763
  • 3
  • 21
  • 40
  • 1
    Awesome! Appreciate the explanation. The auto-inheritance was something I didn't know about, and that clears up a lot. With that reasoning, is it "acceptable" to have my parent view ("Manage") to take my own custom viewmodel, and then where it calls the child view "_ChangePasswordPartial" I can do so like; @Html.Partial("_ChangePasswordPartial", new ManageUserViewModel()) – user1861826 Nov 12 '13 at 03:41