tags:

views:

46

answers:

1

I insert into asp.net mvc views C# logic that manages layout like the following:

<% if (Model.People.Count > 0 ) { %>
   <% foreach (var person in Model.People) { %>      
      ...
<% }} else { %>
   <span class="error">Sorry, no people</span>
<%} %>

I try to minimize <% %> code placing "{" symbol on the same line as it's condition (java-style). Html layout looks more clear to me after that.

Do you apply C# formatting rules to <% %> html injections "}" should be on a new line or manage layout in different way?

Thank you in advance!

A: 

Its totally up to you, whatever you find is more readable and maintainable.

The less inline server blocks you have the better though (in terms of preventing run-time code compilation errors).

RPM1984
I think so. But often html-layout come overmixed with code injected when i apply css classes to elements or when I have to show/hide some blocks that depend on entity attributes or collection sizes. How do you keep this situation maintainable?
Andrew Florko
Again, depends exactly on the scenario. In the above scenario, you want to print out people from the model, and if no people there, show a span.Realistically, you could check the model data in the controller, and return an appropriate view based on that.In other words, you would have one for Results, another for no Results and return the appropriate View in your controller.If you've got too much code in your View, chances are you've got code which needs to be moved to the either the model or the controller.
RPM1984
I came from asp.net web.forms world with ContentPlaceHolder Visible Yes/No html layout driven approach :) I'll take balanced approach choosing between different views and conditional views. Thank you
Andrew Florko
Me too - there is always a temptation to write "asp:" but you have to avoid it.That's why i like MVC - it forces you to understand web development by not hiding the underlying implementation like web forms does. Thick model, light controller, dumb view.That's the approach im taking to MVC applications!
RPM1984