3 回答

TA貢獻1813條經驗 獲得超2個贊
您的代碼對我來說工作正常。
不過,這里有一種更簡潔的寫法??纯此欠駥δ阌杏茫?/p>
<p>
@if (course.Description.Length > 100)
{
@course.Description.Substring(0, 100) @:"..."
}
else
{
@course.Description
}
</p>
如果您仍然遇到問題,那么您的 Razor 頁面中的周圍代碼可能有其他問題,或者您的項目沒有引用正確的程序集?嘗試在一個新的空白 ASP.NET 項目中運行相同的代碼。

TA貢獻1906條經驗 獲得超10個贊
我認為您的 if 語句中的“課程”缺少“@”。嘗試:
<p>
@{
if(@course.Description.Length > 100)
{
@course.Description.Substring(0, 100) @:"..."
}
else
{
@course.Description;
}
}
</p>

TA貢獻1818條經驗 獲得超8個贊
無法決定何時異常使用,例如,如果我這樣使用它
@functions{
public IHtmlContent RenderSubButton(ButtonModel button)
{
var @appIcn = "blabla";
return @Html.Raw(@"<a href='@Url.Action(" + button.Action + ", " + button.Controller + ", " + button.RouteValues + ")' class='" + button.Class + "' " + button.Binding.ToDataAttributes() + ">"
+ button.Text
+ @appIcn + "</a>");
}
}
appIcon 將不起作用,但如果我使用
@functions{
public IHtmlContent RenderSubButton(ButtonModel button)
{
var @appIcn = "blabla";
return @Html.Raw(@"<a href='@Url.Action(" + button.Action + ", " + button.Controller + ", " + button.RouteValues + ")' class='" + button.Class + "' " + button.Binding.ToDataAttributes() + ">"
+ button.Text
+ appIcn + "</a>");
}
}
然后它會工作。區別在于 appIcn 前面有 @this,不需要它
- 3 回答
- 0 關注
- 99 瀏覽
添加回答
舉報