我想在我的_Layout中使用我的ViewBag,因为我所有的视图都有类似的数据。所以在这里我做什么:ViewBag在扩展类中返回null
在我看来:
ViewBag.MetaKeywords = Model.MetaKeywords
我有绑定的的HtmlHelper
public static string MetaKeywords(this HtmlHelper helper)
{
return helper.ViewContext.Controller.ViewBag.MetaKeyWords;
}
在我_layout的扩展类:
@Html.MetaKeywords()
的问题是我的扩展方法返回null。为什么我使用扩展方法而不是@ViewBag.MetaKeyWords
?因为一些其他功能有逻辑,我们想在我们之间分享它。
谢谢你的帮助。
您正在设置'ViewBag.MetaDescription',但您尝试访问您的帮助器中的'ViewBag.MetaKeyWords'是否是您的示例代码中的拼写错误?因为属性名称应该匹配。 – nemesv
对不起,我在我的文章中发了一个错误,我修改了它,所以我的问题依然存在。 – dlanglois6