在布局文件中设置ViewBag元描述

时间:2013-12-02 14:17:12

标签: c# asp.net-mvc

我正在尝试以下方法:

<meta name="description" content="@{ ViewBag.MetaDescription != null ? ViewBag.MetaDescription : "Welcome to the site!" }">

我收到了一个错误:

Only assignment, call, increment, decrement 

为什么这个三元有条件不工作?

2 个答案:

答案 0 :(得分:2)

您可以使用以下内容:

<meta name="description" content='@(ViewBag.MetaDescription ?? "Welcome to the site!")'>

答案 1 :(得分:1)

@{}块不会固有地返回值。 你基本上是这样做的:

function MyFunction()
{
    ViewBag.MetaDescription != null ? ViewBag.MetaDescription : "Welco"...
}

尝试使用@Html.Raw()或使用@语句。