The name 'ViewBag' does not exist in the current context解决思路
The name 'ViewBag' does not exist in the current context
使用VS 2013,学习MVC,显示如下错误:
![The name 'ViewBag' does not exist in the current context解决思路 The name 'ViewBag' does not exist in the current context解决思路](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDE1LzAzLzEwLzEyNTQ0MDI1Mi5wbmc=)
求解决方法。谢谢!
------解决思路----------------------
这是我做的一个测试如下
通过BuildManager可以获取_ViewStart被编译后的类名和程序集的位置
如下
找到这个程序集打开反编译器 我们会发现 实际上_Page_Views__ViewStart_cshtml这个类的基类是ViewStartPage ViewStartPage类 中是没有ViewBag属性的:
![The name 'ViewBag' does not exist in the current context解决思路 The name 'ViewBag' does not exist in the current context解决思路](/default/index/img?u=aHR0cDovL3d3dy5teWV4Y2VwdGlvbnMubmV0L2ltZy8yMDE1LzAzLzEwLzEyNTQ0MDI1My5wbmc=)
而普通的视图是继承的WebViewPage<object> 这里我就不做实验了。 WebViewPage<object>是具有ViewBag属性的。
原理是mvc框架本身就是约定大于配置的_ViewStart这个名字就代表着这个 文件是用来指定 视图的默认模板页的。它并不需要ViewBag编译的时候就会动态生成一个继承于ViewStartPage的类
------解决思路----------------------
试了下没问题啊,你的mvc是几点几版本啊
使用VS 2013,学习MVC,显示如下错误:
求解决方法。谢谢!
------解决思路----------------------
这是我做的一个测试如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Compilation;
using System.Web.Mvc;
namespace MvcApp.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public void Index()
{
Response.Write(BuildManager.GetCompiledType("~/Views/_ViewStart.cshtml") + "<br/>");
Response.Write(BuildManager.GetCompiledAssembly("~/Views/_ViewStart.cshtml").Location + "<br/>");
}
}
}
通过BuildManager可以获取_ViewStart被编译后的类名和程序集的位置
如下
ASP._Page_Views__ViewStart_cshtml
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\vs\36ddc88d\9410a04c\App_Web_wyhjqf3b.dll
找到这个程序集打开反编译器 我们会发现 实际上_Page_Views__ViewStart_cshtml这个类的基类是ViewStartPage ViewStartPage类 中是没有ViewBag属性的:
而普通的视图是继承的WebViewPage<object> 这里我就不做实验了。 WebViewPage<object>是具有ViewBag属性的。
原理是mvc框架本身就是约定大于配置的_ViewStart这个名字就代表着这个 文件是用来指定 视图的默认模板页的。它并不需要ViewBag编译的时候就会动态生成一个继承于ViewStartPage的类
------解决思路----------------------
@{
ViewBag.Title = "abc";
}
试了下没问题啊,你的mvc是几点几版本啊