I've a main view that contains an array received from the corresponding Action and it also contains a partial view reference below
Create.cshtml :
@model HrAndPayrollSystem.Models.EmployeeMasterA
@using (Html.BeginForm())
{
ViewData["fs_lbls"] = ViewBag.FS_lbls as string[];
@Html.Partial("~/Views/EmployeeMasterA/FinalSettlementTAB.cshtml", Model)
}
and the referenced partial view above is defined below
FinalSettlementTAB.cshtml :
@model HrAndPayrollSystem.Models.EmployeeMasterA
@Html.DropDownList("DeptId", null, "Department")
/* Print "ViewData["fs_lbls"]" array defined in the Main View `Create.cshtml` here */
I've an array defined in the Create.cshtml, now, I want to pass it into the partial view HR_EmployeeFinalSettlementTAB.cshtml and print it, What is the proper way to to this?
What I've tried :
I changed the @Html.Partial() line into below :
@Html.Partial("~/Views/EmployeeMasterA/FinalSettlementTAB.cshtml", null, new ViewDataDictionary { { "fs_lbls", ViewData["fs_lbls"] } })
and modified the FinalSettlementTAB.cshtml file as below :
@model HrAndPayrollSystem.Models.EmployeeMasterA
@Html.DropDownList("DeptId", null, "Department")
@foreach (var i in ViewData["fs_lbls"] as string[])
{
@i
}
But it throws an exception InvalidOperationException at line @Html.DropDownList("DeptId", null, "Department") by saying :
There is no ViewData item of type 'IEnumerable' that has the key 'DeptId'.
It throws the above exception whenever I try to pass the array data to the partial view using ViewDataDictionary, otherwise, it is working fine, when I'm not.
How do I get rid of the above exception and properly pass array data from main view to the partial view?