tags:

views:

459

answers:

2

When I use Html.DisplayFor() using a property of the model, it comes out nicely with both a label for the property name and a textbox or label with the property value:

Html.DisplayFor(model => model.FirstName)     // renders as First Name: Joe Smith

But if I try to use the same for something that is in ViewData, it doesn't seem to have any way to specify the text that will be used in the label in the rendered html:

Html.Display(ViewData["something"].ToString())     // renders as (no label) something

The other Html.Display() parameters don't look helpful:

Html.Display(ViewData["something"].ToString(), "TemplateName", "HtmlElementId", {additionalData})

It looks like the only place I might pass the label is with the additionalData param, but I haven't found any examples or docs on how to do this.

A: 

Why are you using ViewData? Can you instead return a composite FormViewModel? That way all your data is in a model and all is well with the world.

griegs
If you want to render model data on the Master page, using the ViewData is pretty much the only way to do this. I'm not sure if this is why JK is using it, but that's one time where you can't use a strongly typed viewmodel.
Alastair Pitts
Yeah it's unclear at this point. @JK can you clarify?
griegs
It's a large MVC 1 app slowly being converted to MVC2. So ViewData is used frequently. Changing to a ViewModel is not going to be done in this phase (will be later). Its a calculated field that is not present in the model that I am trying to display.
JK
+3  A: 

Html.Display() takes a string expression of what to find in the viewdata and not the view data itself.

You want:

Html.Display("something");
jfar
Yes I typed it wrong in the question. But this doesn't answer the question. I know how to display the value, its the label that I cant automatically create with either Label() or LabelFor() (of course I can always manually type in the label text myself)
JK
Sorry meant to say "with either Display() or DisplayFor()" (not Label)
JK