2017-04-06 78 views
0

的我需要添加鏈接打印頁面,但我有代碼錯誤名稱「項目」不在當前情況下存在名稱「項目」不在當前情況下存在了forech

裏面的foreach語句everithing的是好的,但在頁面我需要的圖片和鏈接添加到打印頁面的底部包括ID

這裏是查看代碼:

**<a href = "/rezervacii/[email protected](modelItem => modelItem.relID)"><img src = "~/Content/images/print_icon.png" alt = "Печати" /></a>** 

如何解決這個問題?

編輯:答案是在這裏,我解決這個簡單的代碼。

@foreach (var item in Model.Take(1)) 
    { 

    <a href="/rezervacii/[email protected](modelItem => item.tbl_relacii.relID)"><img src="~/Content/images/print_icon.png" alt="Print" /></a> 

    } 
+0

變化'DisplayFor(modelItem => ..'爲'顯示(項目=> ...)' – Niewidzialny

+0

複製和粘貼我的回答從下面它的工作原理。謝謝@JoeDie –

回答

1

將此代碼行添加到foreach循環中。

@{ counter=0; } 
    foreach() 
    { 
     if(counter==0) 
     { 
    <a href = "/rezervacii/[email protected](modelItem => modelItem.tbl_relacii.relID[0])">< img src = "~/Content/images/print_icon.png" alt = "Print" /></a> 
     } 
} 

希望它有助於

0

替換modelItem這就是你宣佈它的名稱項。

+0

我試過但不工作:( –

1

你不需要@Html.DisplayFor顯示id您可以通過

<a href = "/rezervacii/[email protected]_relacii.relID">< img src = "~/Content/images/print_icon.png" alt = "Print" /></a> 

直接做編輯

,你可以用它來從IEnumerable

@Model.Select(x => x.tbl_relacii.relID).Single(); 

讓你獲得一個ID代碼將是

<a href = "/rezervacii/[email protected](x => x.tbl_relacii.relID).Single()"> 
+0

我試過這個,但我又有錯誤**名稱'項目'不存在於當前上下文中** –

+0

@JoeDie這是因爲你在循環之外使用它你可以分享整個頁面嗎?這將很容易回答 – Usman

+0

@JoeDie你在第二頁發送IEnumerable 嗎? – Usman

0

這是答案的簡單代碼:

@foreach (var item in Model.Take(1)) 
    { 

    <a href="/rezervacii/[email protected](modelItem => item.tbl_relacii.relID)"><img src="~/Content/images/print_icon.png" alt="Print" /></a> 

    } 
相關問題