I’m trying to pass some parameters (a couple of strings) from a page to a partial view that is to be rendered in the main page. To do this, I’m passing an anonymously typed object, which keeps giving me a RuntimeBinderException. Given what I’ve tried, I’m not surprised to be getting the error, but I don’t know what else to try.
Views\Home\PageWithPartialView.cshtml
@Html.Partial("DynamicPartialView", new { paramFromPageToPartialView = "value" })
Views\Shared\DynamicPartialView.cshtml
@model dynamic // Doesn't make a difference
@{
// This is where I need to access and display the parameters
// passed from the main page
// Throws RuntimeBinderException
// Cannot apply indexing with [] to an expression of type 'object'
var try1 = Model["paramFromPageToPartialView"];
// Throws RuntimeBinderException
// 'object' does not contain a definition for 'paramFromPageToPartialView'
var try2 = Model.paramFromPageToPartialView;
}
If partial views aren’t the way to do this, I’m open. The partial view has a couple hundred lines of code to produce, so custom HtmlHelpers don’t seem manageable to me.
The
ViewBagis designed to solve this kind of problem. Rather than consumingparamFromPageToPartialViewin your partial from the Model, consume it from theViewBag:Views\Home\PageWithPartialView.cshtml
Views\Shared\DynamicPartialView.cshtml