Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to set the HideSurroundingHtml value in ASP.MVC 2

I'm building a table of data like this

<% foreach (var person in Model.People)
{
%>
    <tr>
        <td><%= Html.ActionLink(accessory.Name, "EditPerson") %></td>
        <td><%= Html.DisplayFor(c => person.Name) %></td>
        <td><%= Html.DisplayFor(c => person.Age) %></td>
        <td><%= Html.DisplayFor(c => person.Budget)%></td>
    </tr>
<%} %>

I've created templates to override the defaults following Brad Wilson's tutorial:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<script runat="server">
    protected override void OnInit(EventArgs e) {
        base.OnInit(e);

        if (ViewData.ModelMetadata.HideSurroundingHtml) {
            TablePlaceholder.Visible = false;
        }
        else {
            Controls.Remove(Data);
            DataPlaceholder.Controls.Add(Data);
        }
    }
</script>
<asp:ContentPlaceHolder runat="server" id="Data" />
<asp:PlaceHolder runat="server" id="TablePlaceholder">
    <table cellpadding="0" cellspacing="0" border="0" width="100%">
        <tr>
            <td style="width: 10em;">
                <div class="display-label" style="text-align: right;">
                    <asp:ContentPlaceHolder runat="server" id="Label">
                        <%= ViewData.ModelMetadata.GetDisplayName() %>
                    </asp:ContentPlaceHolder>
                </div>
            </td>
            <td>
                <div class="display-field">
                    <asp:PlaceHolder runat="server" id="DataPlaceholder" />
                </div>
            </td>
        </tr>
    </table>
</asp:PlaceHolder>

When rendering the table I don't want to display the surrounding HTML, but I don't have a clue how to set the HideSurroundingHtml value?

like image 330
adriaanp Avatar asked Feb 01 '10 21:02

adriaanp


2 Answers

According to the MSDN page on ModelMetadata.HideSurroundingHtml Property:

When this property is used with the DataAnnotationsModelMetadataProvider model metadata provider, it is set to true when both the HiddenInputAttribute attribute is true and the DisplayValue property is set to false.

So you need to decorate your property in your model with this:

[HiddenInput(DisplayValue = false)]

I doesn't make any sense to me but it seems to work!

like image 78
Johnathan Sewell Avatar answered Nov 18 '22 08:11

Johnathan Sewell


A cleaner and more clear way to do it would be like this:

public class MyModel
{
    [AdditionalMetadata("HideSurroundingHtml", true)]
    public string Something { get; set; }
}

And in your view:

bool hideSurroundingHtml = (this.ViewData.ModelMetadata.AdditionalValues.ContainsKey("HideSurroundingHtml") ? (bool)this.ViewData.ModelMetadata.AdditionalValues["HideSurroundingHtml"] : false);

if (!hideSurroundingHtml)
{
    @:<div>
}

//Content.

if (!hideSurroundingHtml)
{
    @:</div>
}
like image 28
Josh M. Avatar answered Nov 18 '22 06:11

Josh M.