13838

How do I access an anonymously-typed object passed into a partial view?

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.

<strong>Views\Home\PageWithPartialView.cshtml</strong>

@Html.Partial("DynamicPartialView", new { paramFromPageToPartialView = "value" })

<strong>Views\Shared\DynamicPartialView.cshtml</strong>

@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.

Answer1:

The ViewBag is designed to solve this kind of problem. Rather than consuming paramFromPageToPartialView in your partial from the Model, consume it from the ViewBag:

<strong>Views\Home\PageWithPartialView.cshtml</strong>

@{ViewBag.paramFromPageToPartialView = "value";} @Html.Partial("DynamicPartialView")

<strong>Views\Shared\DynamicPartialView.cshtml</strong>

@model dynamic // Doesn't make a difference @{ var try3 = ViewBag.paramFromPageToPartialView; }

Recommend

  • Do lifetime parameters in `*(&'a mut self)` methods confuse the BorrowChecker?
  • Use Hidden field in syncfusion column chart series
  • displaying # views on a page without hitting database all the time
  • boost::format vs sprintf
  • How to get length of a dynamic array in Delphi? [closed]
  • Finding max value of a weighted subset sum of a power set
  • Difficulties implementing the Hysteresis step of Canny Algorithm in Halide without define_extern fun
  • How many percent of the tweets does twitter sample API give?
  • Model binder ValueProvider appends to the existing value + MVC 4
  • Recursion in ASP.NET Core Razor views
  • a concept similar to pointers in as3?
  • MVC extension method error
  • C++ accessing vector
  • Ability to use Bootstrap 3 grid system to specify width of select element
  • F#: In which memory area is the continuation stored: stack or heap?
  • Installing Perl6 and Panda on Ubuntu 15.10. Problems with bootstrap.pl
  • Can my PDF ping my server when it is opened?
  • Access Android Market through SSH tunnel
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Android full screen on only one activity?
  • Typescript - Unable to get 'import' statement to function
  • Get data from AJAX - How to
  • Adding a button at the bottom of a table view
  • Accessing IRQ description array within a module and displaying action names
  • Traverse Array and Display in markup
  • How to get icons for entities from eclipse?
  • Proper way to use connect-multiparty with express.js?
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Load html files in TinyMce
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Linking SubReports Without LinkChild/LinkMaster
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • JaxB to read class hierarchy
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Android Heatmap on canvas or ImageView
  • Conditional In-Line CSS for IE and Others?