I want to pass an object Model.AvailableVerticalType
along with the
expression
and templateName
in the call to the HTML Helper DisplayFor
.
Without passing the object, the DisplayFor()
syntax looks like this:
@Html.DisplayFor(o => offer, MVC.Shared.Views.DisplayTemplates.OfferDetail)
The OfferDetail
template accepts an object of the type Offer
only:
@model DGS.DGSAPI.UI.BusinessModels.Offer
So I need a way to send the AvailableVerticleType
through the ViewData
. Is it possible? What would be the syntax for passing ViewData
in DisplayFor()
?
This helps promote testability and code reuse. When a Controller class decides to render an HTML response back to a client, it is responsible for explicitly passing to the view template all of the data needed to render the response.
While doing this we'll discuss two approaches that can be used to pass data from controllers to views: ViewData and ViewModel. One of the defining characteristics of the MVC pattern is the strict "separation of concerns" it helps enforce between the different components of an application.
Display templates MVC has a bunch of handy helpers that we can use to create our views more efficiently. One such helper are the display templates that are used within views.
We can override the default templates by placing our custom display templates into the path Views/Shared/DisplayTemplates/<type>.cshtml. They are structured like any MVC partial view.
As suggested by user3559349, you can pass an anonymous object into the DisplayFor() method and that get's to be a part of the ViewData dictionary.
In your view:
@Html.DisplayFor(o => offer, "OfferDetail", new {AvailableVerticalType = Model.AvailableVerticalType }
In your OfferDetail template:
(AvailableVerticalType)ViewData["AvailableVerticalType"]
You could also just create a partial view that has a model declared for the AvailableVerticalType and reference that in your main view.
@model AvailableVerticalType
You pass it in ViewDataDictionary
, example below:
// optional: if you don't want to use "AdditionalData" magic string
public static class ViewDataKeys
{
public const string AdditionalData = "AdditionalData";
}
We can use the 2nd overload of DisplayFor
and pass the additional data in ViewDataDictionary
:
@Html.DisplayFor(m => m.MyModel, new ViewDataDictionary { { ViewDataKeys.AdditionalData, "additional-value" } })
And in your DisplayFor
template you can access the ViewDataDictionary
like this:
@{
string additionalData = ViewData[ViewDataKeys.AdditionalData];
/*
* if you need to cast the data type:
* var additionalData = (AdditionalDataType)ViewData[ViewDataKeys.AdditionalData];
*/
}
You can also pass multiple additional data, for example:
@Html.DisplayFor(m => m.MyModel, new ViewDataDictionary {
{ ViewDataKeys.AdditionalData1, "additional-value1" },
{ ViewDataKeys.AdditionalData2, "additional-value2" }
})
Keeping in mind that it is always better to pass the data in a Model.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With