Reputation: 1311
I have a Settings Action on my Account controller that renders the Settings View.
On the Settings View, I recieve a ViewModel that includes ChangePasswordModel.
Here is the SettingsViewModel:
public class SettingsViewModel
{
public ChangePasswordModel ChangePasswordModel { get; set; }
}
The Settings View recieves
@model XBLTools.Models.SettingsViewModel
The ChangePassword View recieves
@model XBLTools.Models.ChangePasswordModel
The ChangePassword view works OK alone. How to render the ChangePassword View passing the Model.ChangePasswordModel?
I've tried some combinations without success getting different errors:
@Html.RenderPartial("ChangePassword", (XBLTools.Models.ChangePasswordModel)(Model.ChangePasswordModel))
@Html.RenderPartial("ChangePassword", Model.ChangePasswordModel)
@Html.Partial("ChangePassword", (XBLTools.Models.ChangePasswordModel)(Model.ChangePasswordModel))
@Html.Partial("ChangePassword", Model.ChangePasswordModel)
Any ideas?
Upvotes: 0
Views: 6834
Reputation: 887413
You can just pass your model property:
@Html.Partial("ChangePassword", Model.ChangePasswordModel)
If the ChangePasswordModel
proeprty is null
, you'll get an error, since the partial view needs a model.
Make sure that you've set the ChangePasswordModel
property to an instance.
Alternatively, you can just pass a new ChangePasswordModel
instance:
@Html.Partial("ChangePassword", new ChangePasswordModel())
Upvotes: 1
Reputation: 35793
If it's null pass in a new instance of ChangePasswordModel:
@Html.RenderPartial("ChangePassword", new ChangePasswordModel())
Or instantiate it in the constructor of SettingsViewModel:
public class SettingsViewModel
{
public SetttingsViewModel()
{
ChangePasswordModel = new ChangePasswordModel();
}
public ChangePasswordModel ChangePasswordModel { get; set; }
}
Upvotes: 3
Reputation: 22016
You should initialise the ChangePasswordModel on the settings view model in the controller.
public ActionResult MyAction()
{
var model = new SettingsViewModel{
ChangePasswordModel = new ChangePasswordModel()
}
return View(model);
}
then use:
@Html.Partial("ChangePassword", Model.ChangePasswordModel)
Upvotes: 1