Reputation: 3862
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.
Upvotes: 0
Views: 1223
Reputation: 3862
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.
Upvotes: 1
Reputation: 6659
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()); %>
<% } %>
Upvotes: 1
Reputation: 100567
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.
Upvotes: 1