Not display/write specific html if model.entity is null

Not display/write specific html if model.entity is null

Normally I work in ASP Classic, and am still a bit green on ASP.Net MVC.

I have my model with its fields or entities (might not be the right word hence why not able to find if previously answered, forgive me), and some of these will have null values such as hyperlinks, email address, specific types of phone numbers, etc.

What I want to do is not write certain blocks of HTML within the foreach loop if a field is empty.

In ASP Classic, I would have written an If statement along the lines of:

<% If RS.field <> NULL then %>HTML with <%=RS.field %><% End If %> <% If RS.field2 <> NULL then %>HTML with <%=RS.field2 %><% End If %> ... 

However, I don't know how to achieve the same result in ASP.Net MVC.

In one of my current usages, a list of sellers returned for a specific set of search results, it runs through a foreach loop and returns the current HTML (in part):

Website: <a class="url" href="<%= seller.ContactWebsite %>" target="_blank" title="Visit the website for  <%= seller.Name %>"><%= seller.ContactWebsite %></a><br /> 

So, if for example I have a field from the model, say seller.ContactWebsite, and this field was NULL, I would not want it to write this as part of the foreach loop.

All answers and assistance appreciated.

Test Function in MVC Project


WebHost4Life host migrated my .NET MVC site and now membership functionality does not work
If you are using the WebForms view engine with C# here's how your code might look like:. SSI-like feature in ASP.NET / ASP.NET MVC
<% if (!string.IsNullOrEmpty(Model.field1)) { %>     HTML with <%= Model.field1 %> <% } %> 
This assumes this your view is strongly typed to a model class and this the controller action provided to the view an instance of this model.. how do i load thousands of rows in my mvc project from database into slickgrid? As you must see this syntax turns quickly into a tag soup as mixing markup with server side code is ugly. HTML.DropDownList values from multiple sources?To solve this you could write a custom HTML helper method this will be responsible for properly formatting.. Authentication for IIS content in virtual directory under ASP.NET MVC website Let's take for case this snippet:. How can I get the client side date and currency formats?
<a class="url" href="<%= seller.ContactWebsite %>" tarreceive ="_blank" title="Visit the website for  <%= seller.Name %>"><%= seller.ContactWebsite %></a> 
Here's how a potential helper method might look like:. When to use Singleton vs Transient vs Request using Ninject and MongoDB
public static MvcHtmlString MyCustomLink(this HtmlHelper htmlHelper, SellerViewModel seller) {     if (seller == null)     {         return MvcHtmlString.Empty;     }     var anchor = new TagBuilder("a");     anchor.AddCssClass("url");     anchor.MergeAttrialthough e("href", seller.ContactWebsite);     anchor.MergeAttrialthough e("tarreceive ", "_blank");     anchor.MergeAttrialthough e("title", "Visit the website for " + seller.Name);     anchor.SetInnerText(seller.ContactWebsite);     return MvcHtmlString.Create(anchor.ToString()); } 
which could be used like this in the for loop:.
<%= Html.MyCustomLink(seller) %> 

71 out of 100 based on 16 user ratings 541 reviews