2012-02-29 55 views
1

這是我的html代碼:描述:在編譯爲請求提供服務所需的資源編譯期間發生錯誤。編譯器錯誤信息:CS1513:}預期

<span> 
<%if (Model.Data.Service.Attachments.Count > 0) 
{ 

%><h3>Downloads for your service:</h3> <% 

foreach (var attach in Model.Data.Service.Attachments) 
{ 
    %><%=attach.Name%>: https://<%= Request.Url.Host%> "/File/Download/" <%= attach.Id.ToString()%><br /> 
}<% 

}%></span> 

錯誤是說我缺少一個「{」,但我不認爲這是什麼回事

回答

1

您的foreach的右括號不在<%%>塊。它需要:

<span> 
<%if (Model.Data.Service.Attachments.Count > 0) 
{ 

%><h3>Downloads for your service:</h3> <% 

foreach (var attach in Model.Data.Service.Attachments) 
{ 
%><%=attach.Name%>: https://<%= Request.Url.Host%> "/File/Download/" <%= attach.Id.ToString()%><br /> 
<% } 

}%></span> 

還是要使它有點整潔嘗試使用的String.format來代替:

<span> 
<%if (Model.Data.Service.Attachments.Count > 0) 
{ 
%><h3>Downloads for your service:</h3> <% 
foreach (var attach in Model.Data.Service.Attachments) 
{ 
    %><%= string.Format("{0}: https://{1}/File/Download/{2}", attach.Name, Request.Url.Host, attach.Id) %><br /> 
<%} 
}%></span> 
+0

,這裏不是問題。他有語法錯誤。 – 2012-02-29 15:45:32

+0

已更新爲包含解決方案。 – 2012-02-29 15:47:59

+0

您的string.Format片段仍然存在錯誤。 – 2012-02-29 15:49:02

1

但我不認爲這是什麼回事

你最好相信編譯器。我建議你適當地縮進您的代碼,這樣的語法錯誤很容易看出:

<span> 
<% if (Model.Data.Service.Attachments.Count > 0) { %> 
    <h3>Downloads for your service:</h3> 
    <% foreach (var attach in Model.Data.Service.Attachments) { %> 
     <%= attach.Name %>: https://<%= Request.Url.Host %> "/File/Download/" <%= attach.Id.ToString() %> 
     <br /> 
    <% } %> 
<% } %> 
</span>