I am aware that views should not have code in them but in a project I am working on I have a lot of logic in the views.
My home page has
<% Html.RenderPartial("SearchResults"); %>
Now in the partial view I have an aweful lot of logic like this;
<div id="RestaurantsList">
<%if (Model.restaurantsList.Count() > 0)
{
foreach (var item in Model.restaurantsList)
{ %>
<% Html.RenderPartial("SearchResult", item); %>
<%
} %>
<%
}
else
{
Html.RenderPartial("NoResults");
} %>
Now I could make the home controller return a different view based on the list being empty but I don't really want that as the Index view has a few things that I want displayed no matter if there are results or not.
The only other thing I can think of here is to encapsualte this in a helper method like Html.SearchResults. But then I would need the helper to call the renderPartial for each search result also. That doesn't seem like clean seperation of concerns.
I would still have to have the first if statement in the partial view though.
How would you best handle this?