Pass Additional ViewData to DisplayFor Template
Asked Answered
P

2

11

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

Plerre answered 10/6, 2016 at 7:11 Comment(5)
Is the object part of your model, or have you added in using ViewBag of ViewData in the GET method? Note that its this overload that you wantDespotic
Model.AvailableVerticalType is part of the modelPlerre
To pass it you would use @Html.DisplayFor(o => offer, MVC.Shared.Views.DisplayTemplates.OfferDetail, new { data = Model.AvailableVerticalType } ) and then in the template you can access it using (yourType)ViewData["data"]Despotic
@StephenMuecke Thanks!Plerre
@StephenMuecke, if that's the answer, please post it as an answer.Porridge
D
7

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

Drusi answered 27/3, 2020 at 17:21 Comment(0)
B
2

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.

Brimmer answered 27/6, 2020 at 4:56 Comment(1)
To me, this is a bit of an XY problem, which the last line of your question really gets to the heart of. The OP is asking about the ViewData because they don't know how to pass the data as part of their model. Given that, is there a way to extend the model while still relying the expression bindings necessary for the DisplayFor() template to properly assess e.g. validation attributes on the original target property? (I'm not certain there is, which thus necessitates the use of ViewData.)Fritts

© 2022 - 2024 — McMap. All rights reserved.