I've tried
<%: Html.DisplayFor(m => m.Overall) %>
<%: Model.Overall %>
and neither work and may only ideas right now would be to make if statments on the view itself that if it was null not to display but I have 开发者_开发知识库over 200 values so time consuming and wasteful, my other idea was to do the same but in jquery document.ready the if statements.
does anybody have any simple ideas or css solutions which would prevent a mass of white space.
thx
If you are using MVC 3 using display templates you can create a loop over the properties. So similar to another answer, create a display template for NotEmptyProperties.ascx and in your page call:
<%: Html.DisplayForModel("NotEmptyProperties") %>
And then in the display template:
foreach(var p in ViewData.ModelMetadata.Properties)
{
if(ViewData.Eval(p.PropertyName) != null)
{
<%: Html.Display(p.PropertyName) %>
}
}
The Eval
method on the ViewDataDictionary
will let you null check the properties in your loop and only display the ones that aren't null.
In this case I would use custom Display Templates. It would leave your view very clean, and your if/else clause in one location. Your displaly templates would look something like this (CheckEmpty.ascx):
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<String>" %>
<%= String.IsNullOrEmpty(Model) ? String.Empty : Model %>
Then you have two options of where to specify the Display Template, In your view:
<%: Html.DisplayFor(m => m.Overall, "CheckEmpty") %>
or using data annotations in your model:
//model
[UIHint("CheckEmpty")]
String Overall {get; set;}
<%-- View --%>
<%-- Don't need to specify template, since UIHint already specifies --%>
<%: Html.DisplayFor(m => m.Overall) %>
Have u tried something like below :
<% if(Model != null) { %>
<%: Html.DisplayFor(m => m.Overall) %>
<% } %>
精彩评论