Posts tagged: razor

May 06 2012

How do I include ul tags inside of a razor code block?

How come the following code works fine...

        <ul class="searchList">
            @if (Model.Count() > 0)
            {
                foreach (var partner in Model)
                {
                    <li>
                        @Html.ActionLink(@partner.Name, "Details", "Partner", new { id = partner.AID }, null)<br />
                        @partner.Street<br />
                        @partner.CityStateZip<br />
                        @if(!string.IsNullOrEmpty(partner.Phone))
                            {
                               @partner.Phone<br />
                            }
                        @(partner.Distance) miles<br />
                    </li>
                }
            }
        </ul>

But this code does not work fine...

            @if (Model.Count() > 0)
            {
                <ul class="searchList">

                        foreach (var partner in Model)
                        {
                            <li>
                                @Html.ActionLink(@partner.Name, "Details", "Partner", new { id = partner.AID }, null)<br />
                                @partner.Street<br />
                                @partner.CityStateZip<br />
                                @if(!string.IsNullOrEmpty(partner.Phone))
                                    {
                                       @partner.Phone<br />
                                    }
                                @(partner.Distance) miles<br />
                            </li>
                        }

                </ul>
             } 

The second error one returns the following error...

Compiler Error Message: CS0103: The name 'partner' does not exist in the current context.

I am finding the code mixing rules of Razor to be difficult to follow. Any link that gives the canonical explanation will be appreciated.

Seth

May 04 2012

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

May 02 2012

How do you pass "expensive" data from page to page using ASP.NET MVC 3?

I am doing my first ASP.NET MVC project. (In fact, for the record, this is my first production website of any kind).

This is a mobile web page targeting HTML 5.

This page looks up some "expensive" information. First it uses the html 5 geocoding feature to get the customers latlong from their browser.

I pass that information to a controller. That controller fills in the City and State (into a location model) using the Google Maps API and then uses it in the view.

So this data is expensive in two ways, first it takes time to look it up and second, in the case of the Google API, it is literally not free in that Google limits the number of calls that can be made per day.

I understand that MVC is designed to "embrace" the web including the fact that http is a stateless protocol.

Nevertheless, I would like to avoid having to fetch this expensive data on every page load for every endpoint that needs it. Furthermore, one other piece of state that I would like to keep track is the fact that the data has already been fetched.

What is the best way / best practice for achieving this on an MVC 3 web application? Since my model for location has 4 data point (lat long city state) and there is a fifth data point (data retrieved) I would really like to avoid using querystrings to do this or a route for all of those data points?

I am sure this is a common need but I honestly don't know how to tackle it. Any help will be appreciated.

Seth