I need to pass some information to a partial cshtml view. I use a ViewDataDictionary
and the ViewBag
for that.
public ActionResult TaskPage(int ID = 0)
{
ViewBag.breadcrumbs = new string[] {
"List", "Tasks"
};
ViewBag.title = "Editing - My selected itemd";
ViewBag.id = ID;
return View();
}
and then when assembling the view I pass a ViewDataDictionary
as well:
@Html.Partial("~/Views/Components/_Header.cshtml", new ViewDataDictionary { { "role", "task-page" } })
My _Header.cshmtl
file uses both this dictionary and the ViewBag
.
ViewBag.breadcrumbs = ViewBag.breadcrumbs != null ? ViewBag.breadcrumbs : new string[] { };
var role = ViewData["role"];
To my surprise, the ViewBag
does not contain any data when I pass the ViewDataDictionary
as well. How is that possible? Any idea how to better handle this?