views:

184

answers:

3

I've run into a couple different exceptions with this block of code in one of my views:

<% if (Model.Book.ReviewReference == null)
   {%>
       <%=Html.ActionLink("Rate / review this book", "Create", "Review", null, new { id = "reviewLink" }) %>
      <% Html.RenderPartial("CreateReview");
   }
   else
   {%> 
       <%= Html.ActionLink("Edit this book's rating / review","Edit", "Review", new { reviewID = Model.Book.ReviewID}, new {id = "reviewLink"}) %>
        <% Html.RenderPartial("EditReview", Model.Book.Review, new ViewDataDictionary());
   }  %>

The first error I encountered was described here: link text

thus the Html.RenderPartial("EditReview", Model.Book.Review, new ViewDataDictionary()) you see towards the end there.

Another problem I encountered is when the if condition is evaluated for a ReviewReference that is in fact null, the else statement is still being reached somehow, and the second partial view is making an unsuccessful attempt to render itself.

Have I used these alternating inline-code tags in an incorrect manner? How does one go back and forth between <% %> and <%= %> properly?

Thank you.

Edit:

OK, I marked an answer too soon. I just tried it with the given code from the answer, and that else block is still being evaluated, and trying to pass null objects to the partial view...darn it.

+1  A: 

You are correct in your usage of the tags there.

They aren't 2 alternating styles, but differences in how the view engine deals with different statements. It's not surprising that it can get confusing.

<%= : Think of this as Response.Write(). You supply it with a string.

<% Html.RenderPartial - this is a command given to the view engine to actually render a partial view. You're not giving it a string, but rather telling the view to go get another snippet (partial view), and write it out.

p.campbell
+1  A: 

Missing a couple of closing %>

<% if (Model.Book.ReviewReference == null)
   {%>
       <%=Html.ActionLink("Rate / review this book", "Create", "Review", null, new { id = "reviewLink" }) %>
      <% Html.RenderPartial("CreateReview"); %>
   <%}
   else
   {%> 
       <%= Html.ActionLink("Edit this book's rating / review","Edit", "Review", new { reviewID = Model.Book.ReviewID}, new {id = "reviewLink"}) %>
        <% Html.RenderPartial("EditReview", Model.Book.Review, new ViewDataDictionary()); %>
   <% }  %>
Christopher Edwards
Doh! :) Cheers Robert!
Christopher Edwards
OK, thanks a lot for straightening me out on this one. Why those closing tags are needed makes no sense to me, though. That's probably why I loused it up to begin with.
PolishedTurd
That's a hell of a lot of tags, now that I look at it again. Sheesh!
PolishedTurd
A: 

OK, so it turns out my tag usage was fine, but my if condition was off. It needed to be:

if (Model.Book.ReviewReference.EntityKey == null)

I was missing the EntityKey property.

PolishedTurd