Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding spaghetti code in ASP.NET MVC

Probably a stupid question, but here goes. In my view, I have the following code....

   <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Learner.MvcApplication.ViewModels.PagerViewModel>" %><% 

for (int i = Model.StartPage; i <= Model.EndPage; i++)
{
    %>
    <% =Html.Label(ViewData.Model.Controller + i.ToString()) %>
    <%
} %> 

Do I have to close and reopen around the call to Html.Label "%><%" etc.?

I'd much rather do something like...

for (int i = Model.StartPage; i <= Model.EndPage; i++)
{
    Html.Label(ViewData.Model.Controller + i.ToString());
} 

...but the labels aren't being displayed.

Can you guess that I'm new to this??

Many thanks,

ETFairfax.

like image 1000
ETFairfax Avatar asked Nov 06 '09 12:11

ETFairfax


2 Answers

<%=x %> is just a shortcut for Response.Write(x):

for (int i = Model.StartPage; i <= Model.EndPage; i++)
{
    Response.Write(Html.Label(ViewData.Model.Controller + i.ToString()));
}
like image 146
Darin Dimitrov Avatar answered Sep 28 '22 10:09

Darin Dimitrov


This is just a short-tag <%= for <% Response.Write note the difference between <% and <%=

So you could very well write it like this:

for (int i = Model.StartPage; i <= Model.EndPage; i++)
{
    Response.Write(Html.Label(ViewData.Model.Controller + i.ToString()));
}

One could argue which is nicer..

like image 38
Filip Ekberg Avatar answered Sep 28 '22 11:09

Filip Ekberg