80698

Pass parameter to Partial View in ASP.NET Core

Question:

On an ASP.NET Core 2.0 application I need to render a partial view and pass a few parameters:

@Html.Partial("Form", new { File = "file.pdf" })

On the partial view I tried to access it using:

@Model.File

And I get the error:

RuntimeBinderException: 'object' does not contain a definition for 'File'

If I simply use on my partial:

@Model

I get the following printed on the page:

{ File = file.pdf }

So there the model is being passed and there is a property File in it.

So what am I missing?

Answer1:

You are passing untyped <em>(anonymous type)</em> data to partial view. You cannot use @Model.File. Instead, you will need to use ViewData's Eval method to retrieve the value.

@ViewData.Eval("File")

Traditional approach is to create a strongly typed <strong><em>ViewModel</em></strong> class, and pass it to the partial view. Then you can access it as @Model.File.

public class SampleViewModel { public string File { get; set; } } @Html.Partial("Form", new SampleViewModel { File = "file.pdf" })

Inside Partial View,

@model SampleViewModel

@Model.File

Answer2:

You should have dynamic as the model of your partial view, this way, you can pass everything - like your anonymous object - and it will just work. Add:

@model dynamic

To the Form.cshtml file.

Answer3:

When you do new { File = "file.pdf" }, you are passing an object that contains an attribute file. Since this is of type object, you can't access any of its variables in c# directly. There some ugly codes to access a fields from an object such as the one that can be found here: <a href="https://stackoverflow.com/questions/42689283/c-sharp-net-core-how-to-get-the-value-of-a-custom-attribute" rel="nofollow">C# .NET CORE how to get the value of a custom attribute?</a>

However in this case the most recommended way (for safety) is the create a class and pass an object of that class.

So if you create the following class:

public class MyFileInfo{ public string File { get; set } }

Then you can create your object by passing:

@Html.Partial("Form", new MyFileInfo{ File = "file.pdf" })

In your partial view, at the beginning, define your model class

@model MyFileInfo

Then in the same file you will now be able to access

@Model.File

Recommend

  • Can you recommend a good php Gantt charting development?
  • Low Accuracy with static image on TFLite demo model
  • pyodbc fails without error
  • Simple Donut Chart with Raphael
  • Multiple JSliders reacting to each other to always equal 100 percent
  • Facebook like button redirect? [closed]
  • Dynamic reference casting depending on actual object type
  • Special characters (ë) in JSON-response
  • Preparing an Oracle Connection after being retrieved from a ConnectionPool
  • Windows: How do I get the mode/access rights of an already opened file?
  • characters not allowed in DOM ids by spec, and by browser
  • Get name of days between two date in ios?
  • content must have a ListView whose id attribute is 'android.R.id.list'
  • playing mp3 from nsbundle
  • Run a form (insert/update/delete) from within a div using jquery
  • How to change the host IP sent in emails to new GitLab users to a publicly visible IP, not the local
  • python selenium scraping tbody
  • Visual Studio 2017 Professional- Unable to find package at source
  • Set SelectedIndex of ListView in FlipView_SelectionChanged event
  • Reload Page with Javascript after Database changes
  • How to redirect into different page by user type in php and mysql
  • Disable account chooser FirebaseUI React
  • ASP.NET MVC 2 actions for the same route?
  • How do I use libcurl to printf a remote FTP directory listing?
  • Multiplying polynomials/simplifying like terms
  • How to define something in JavaScript [closed]
  • JQuery: Infinite input select
  • Amazon Elastick BeanStalk error: Failed to create the AWS Elastic Beanstalk application version
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • Google App Engine Datastore: Dealing with eventual consistency
  • How to decleare char *const argv[] in swift [duplicate]
  • ssh remote server login script
  • Grails - How to implement a foreign key relationship not using an id column?
  • Passing value from popup window to parent form's TextBox
  • How to call different template for different category archive page in woocommerce
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • WPF custom control and direct content support
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication