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" i开发者_JAVA技巧d="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?
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!
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>
}
精彩评论