How to Pass View Data to Partial View in Asp.net core?
The issue is that you've got double quotes inside the view-data
attribute. You need to use single quotes around the attribute value.
<partial name="_Emplyees" model="Employees" view-data='@new ViewDataDictionary(ViewData) { { "index", index } }'/>
Also, @Model
is superfluous here, so I removed it.
You could pass the ViewData to partial view like below in ASP.Net Core MVC:
1.Model:
public class TestModel
{
public string Employees { get; set; }
}
2.View(Create.cshtml):
@model TestModel
@{
ViewData["index"] = true;
}
<partial name="_Emplyees" model="@Model" view-data="ViewData" />
3.Partial View:
<h3>Index: @ViewData["index"]</h3>
@model TestModel
@if ((bool)ViewData["index"])
{
@Model.Employees
}
else
{
<input asp-for="Employees" type="number" class="form-control" />
}
4.Controller:
public IActionResult Create()
{
var testmodel = new TestModel() { Employees = "aaa" };
return View(testmodel);
}
5.Result:
Reference:
How to use view-data pass the data to partial view
https://docs.microsoft.com/en-us/aspnet/core/mvc/views/partial?view=aspnetcore-3.0#access-data-from-partial-views