2011-12-28 23 views
0

我有以下代碼:試圖用剃刀使用索引來循環

@{ 
     var index=0; 
     } 
     @foreach (AdminSummary adminSummary in @Model.AdminSummaries) { 
     index++; 
     <div class="rep_tr0"> 
      <div class="rep_td0">@adminSummary.RowKey</div> 
      <div class="rep_td0"><a href="/Administration/Products/Edit?&[email protected]&[email protected]&[email protected]&[email protected]">Edit</a></div>    
      <div class="rep_td0"><a href="/Administration/Products/Delete?&[email protected]&[email protected]&[email protected]&[email protected]">Delete</a></div>    
      <div class="rep_td0">@Html.TextBox("position_" index, @adminSummary.Position, new { size = 5 })</div> 
      <div class="rep_td0">@adminSummary.Title</div> 
      <div class="rep_td0">@adminSummary.DetailCount</div> 
      <div class="rep_td0">@adminSummary.Modified</div> 
      <div class="rep_td0">@adminSummary.ModifiedBy</div> 

     </div> 
    } 

我想找到一個好辦法,一個索引值添加到位置的名稱。

但是,這一直給我的錯誤:CS1026:)預計在

與position_行有沒有人有任何想法我可能是做錯了什麼?

更新:

我試過如下:

<div class="rep_td0">@Html.TextBox("position_"@(index), @adminSummary.Position, new { size = 5 })</div> 

這給了錯誤:CS1646:關鍵字,標識符或字符串的預期逐字符後:@

+0

你能給我們多一點的錯誤對話框嗎? – 2011-12-28 07:03:36

回答

3

嘗試

<div class="rep_td0">@Html.TextBox(string.Format("position_{0}", index), @adminSummary.Position, new { size = 5 })</div> 
1

嘗試包裝你的插入剃鬚刀()

<a href="http://mystuff/@(Model.Object.Stuff)/Screen">Example</a> 

此外,有時,如果你在剃刀塊,編譯器需要你來包裝的HTML,如果是就行了嘛,像div標籤,在

<text><div class="rep_tr0"></text> 

編輯:對不起,看錯了一點問題,指數++

string textBoxId = "position_" + index; 

再經過:

<div class="rep_td0">@Html.TextBox(@textBoxId, @adminSummary.Position, new { size = 5 })</div> 
3
<div class="rep_td0">@Html.TextBox("position_"+ index.ToString(), @adminSummary.Position, new { size = 5 })</div> 

只是用上面的代替這一行。

0

不錯,但是@foreach(),請嘗試使用@for(),那麼你的循環有它自己的索引。