I am trying to share a form across multiple views using PartialView using the piece of code below:
This is the model I want to share across all views that implement the partial view
namespace CSharp.Models.ViewModels
{
public class HomeViewModel
{
public string County { get; set; }
public ElectionType? Type { get; set; }
}
}
Partial View file looks like this:
@model CSharp.Models.ViewModels.HomeViewModel
@Html.TextBoxFor(model => model.County, new { @class = "form-control" })
@Html.EnumDropDownListFor(model => model.Type, null, new { @class = "form-control"})
In one of the files that need to implement the partial view, I have the following code:
Home View
@model CSharp.Models.ViewModels.HomeViewModel
@using (Html.BeginForm("Index", "Result", new { ViewBag.ReturnUrl }, FormMethod.Get, new { role = "form" }))
{
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.Partial("~/Views/Shared/_PartialViewFile.cshtml", Model)
}
When I run the page, it works as it should.
One the other page that requires the partial view, I have this view that uses a ViewModel
Manage View
@model CSharp.Models.ViewModels.HomeManageViewModel
<form >
@Html.Partial("~/Views/Shared/_PartialViewFile.cshtml", Model.HomeViewModel);
</form>
HomeManageViewModel
looks like this
public class HomeManageViewModel
{
public HomeViewModel HomeViewModel { get; set; }
public IndexViewModel ManageViewModel { get; set; }
}
When I run Manage View
, I get this error:
The model item passed into the dictionary is of type 'HomeManageViewModel', but this dictionary requires a model item of type 'HomeViewModel'
I thought since I'm actually passing Model.HomeViewModel
in Manage View
partial view, it should work.
How can I pass a view model variable to partial view?