2012-10-24 30 views
0

我有一個if/else語句在我的詳細信息視圖,像這樣:爲什麼這個鏈接不會渲染?

@if (Model.SomeProperty != null) 
    { 
     Html.RenderAction("MethodName", "ContollerName", new {id=stuff}); 
    } 
    else 
    { 
     <span>Is this showing?</span> 
     Html.ActionLink("Link Text", "MethodName", "ContollerName", new { Id = something}, null); 
    }     

跨度呈現,所以我知道else塊被擊中,但ActionLink的不會出現。不過,如果我將它移出else塊的這個樣子,它的工作原理:

@Html.ActionLink("Link Text", "MethodName", "ContollerName", new { Id = something}, null) 

我猜它與我的語法不對,但我沒有看到它。

回答

2

Html.ActionLink()返回包含<a>標籤的字符串(實際爲IHtmlString)。

你沒有對該字符串做任何事情。

您需要通過書寫@Html.ActionLink(...)將字符串打印到頁面。 (你仍然可以在代碼塊中做到這一點)

+0

好的。我在if區塊內的html.RenderAction helper上有一個@符號時出錯,所以我(錯誤地)認爲我需要將它從html.ActionLink助手中以及else區塊中移除。我需要買一本書!謝謝。 –

1

答案在於@!只需添加它。

1

它應該是:直接與流

@if (Model.SomeProperty != null) 
{ 
    Html.RenderAction("MethodName", "ContollerName", new {id=stuff}); 
} 
else 
{ 
    <span>Is this showing?</span> 
    @Html.ActionLink("Link Text", "MethodName", "ContollerName", new { Id = something}, null); 
} 

Html.RenderAction作品,因此並不需要它,但Html.ActionLink回報MvcHtmlString這需要「到位」來outputed。