Reputation: 5505
I have a model property:
[HiddenInput(DisplayValue = false)]
public string MyProperty { get; set; }
When I display the property, nothing is shown:
@Html.Display("MyProperty")
Note that as this is for display, I'm not attempting to use:
@Html.Editor("MyProperty")
It shows the value if I use:
@Html.Value("MyProperty")
...but this bypasses the DisplayTemplates.
I'm guessing that the MVC rendering logic has something to stop displaying values with my aforementioned [HiddenInput]
attribute, but if I wanted to do that explicitly I would use [ShowForDisplay(false)]
.
Any ideas?
Upvotes: 0
Views: 1119
Reputation: 1
You can try just [HiddenInput] instead of [HiddenInput(DisplayValue = false)]. This will automatically show the field on display and edit as a label. Hope this helps.
Upvotes: 0
Reputation: 25214
The Display
function "Returns HTML markup for each property in the object that is represented by a string expression" (see http://msdn.microsoft.com/en-us/library/ee310174(v=vs.98).aspx).
What this means is that if your field is a hidden one it will indeed spit out the markup, but because the markup is that of a hidden field you won't see anything directly on the page. You will get something along the lines of <input type="hidden" value="your value" />
. If you look in the page code you will see that the markup is there.
If you want to display just a value on your page, you can simply do @Model.Property
. If you want to input the value into a control you can call any one of many @Html
-based controls and input Model.Property
as the default value.
Personally, I don't like the idea of defining how a page should look in a model. For me, the value itself should be managed by the model but how it is displayed should be left up to your view itself.
I understand that you are working with a display and not an input view here. Regardless of that though, the HiddenInput
attribute simply states that when an editor is rendered for the field (such as with @Html.EditorFor()
), it should be a hidden input HTML element. If you are not trying to render an editor then this attribute has no bearing on the result and you can simply do @Model.Property
to render the element onscreen as I mentioned earlier.
Upvotes: 3