Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Html.Display() using ViewData?

Tags:

c#

asp.net-mvc

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.

like image 669
JK. Avatar asked Jul 21 '26 01:07

JK.


1 Answers

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

You want:

Html.Display("something");
like image 68
John Farrell Avatar answered Jul 22 '26 16:07

John Farrell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!