Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Additional ViewData to DisplayFor Template

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()?

like image 708
Multi stack Avatar asked Jun 10 '16 07:06

Multi stack


People also ask

Why do I need to pass data to the view template?

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.

How do I pass data from controllers to views?

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.

What are display templates in MVC?

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.

How to override default MVC display templates?

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.


2 Answers

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

like image 146
Juan Emmanuel Afable Avatar answered Nov 10 '22 17:11

Juan Emmanuel Afable


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.

like image 25
Hooman Bahreini Avatar answered Nov 10 '22 17:11

Hooman Bahreini