Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Mvc 3 razor view what is the best way to conditionally render html based on Nulls in the model

This is I am sure an easy question but I am having trouble figuring this out.

I want to do something like this....

@(string.IsNullOrEmpty(Model.CustomerUrl) ? "" : <a href="@Model.CustomerUrl">Click me</a>)

This snippet doesn't work.

The mixing of the html with the razor syntax and the inclusion of quotes in the attributes of the tags is making it hard to figure out.

I love razor except figuring out this kind of stuff is really tripping me up.

I would love to just not render the following at all if the CustomerUrl was null or empty...

<p class="customerLink links"><a href="@Model.CustomerUrl">@Model.CustomerName</a></p>

EDIT
This is still not working for me...thanks for the suggestion though.

My issue is that the above code is ALREADY in a Razor Code Block. Here is my actual code that I cannot figure out...

EDIT NUMBER TWO - THE following code is now working

    @if (Model.Count() > 0)
    {
        foreach (var partner in Model)
        {
            <li>
                @Html.ActionLink(@partner.CustomerName, "Details", "Customer", new { id = Customer.AID }, null)<br />
                @partner.Street<br />
//this is what i cannot figure out!!
                @if(!string.IsNullOrEmpty(partner.Phone))
                    {
                        @partner.Phone@:<br />
                    }
                @partner.Distance<br />
            </li>
        }
    }

I preceded the nested block (the if) with the @ symbol. Then the markup
I had to delimit with @: Then it worked.

It seems yesterday when I tried to use a nested razor code block I got a compiler error BECAUSE I preceded it with an @. So now I am more confused than ever.

In fact...if I tried to surround my @partner.Phone with quotes like this... "@partner.Phone"@:</ br> I get another compiler error. Razor is great when it works but when it doesn't it is very confusing.

Seth

like image 603
Seth Spearman Avatar asked May 04 '12 14:05

Seth Spearman


People also ask

What symbol is used by the razor engine in MVC to automatically encode HTML output?

The Razor uses the @ symbol to transition from HTML markup to the C# code.

What is Razor in MVC What are the main Razor syntax rules?

Razor is a simple programming syntax for embedding server code in web pages. Razor syntax is based on the ASP.NET framework, the part of the Microsoft.NET Framework that's specifically designed for creating web applications.

Which keyword of Razor syntax is used to render the view content in the layout page as body content?

Inside the _Layout. cshtml file there is a method call: RenderBody() . This method specifies the point at which the content from the view page is rendered relative to the layout defined.

What is razor view in MVC?

A view is an HTML template with embedded Razor markup. Razor markup is code that interacts with HTML markup to produce a webpage that's sent to the client. In ASP.NET Core MVC, views are .cshtml files that use the C# programming language in Razor markup.


2 Answers

Don't do an inline if.

@if(!string.IsNullOrEmpty(Model.CustomerUrl))
{
    <a href="@Model.CustomerUrl">Click me</a>
}

'Nuff Said

like image 109
Thinking Sites Avatar answered Oct 05 '22 13:10

Thinking Sites


 @if (Model.Count() > 0)
 {

Presumably before this line you had html display, so to denote to razor you're using code, you need the @ symbol.

    foreach (var partner in Model)
    {

You're within a code block already, so the @ symbol wouldn't work here.

        <li>

By using an html tag, razor realizes you're displaying HTML again. All content within here is assumed to be HTML. If you want to tell Razor you have code within here, you need to use the @ symbol to denote the code.

            @if(!string.IsNullOrEmpty(partner.Phone))
            {
                @partner.Phone@:<br />
            }

This is correct because you need to tell Razor you're using code again. Note if this if was directly ABOVE your list tag, you wouldn't use the @ symbol here, because you don't use the @ symbol when you're already within code.

"@partner.Phone" doesn't work for the same reason

if(something)
    ""

wouldn't work in C#. You're creating an object within code without using it.

Hope that helps explain it.

like image 30
DMulligan Avatar answered Oct 05 '22 11:10

DMulligan