为什么我不能在课堂上使用ViewBag。
名称“ ViewBag”在当前上下文中不存在
我用它来存储值
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
使用我用的
using Shop.Data.Migrations.IServices;
using Shop.Data.Models;
using Shop.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using System.Data.Entity;
using System.Web.Mvc;
这不是控制器类
答案 0 :(得分:0)
using System.Web.Mvc;
足以在您的Controller类中使用ViewBage:
ViewBag.Message = "Your application description page.";
您能为我们提供更多信息吗?
答案 1 :(得分:0)
注意: Your Controller class must derives from ControllerBase
使用ViewBag
ViewBag
属于System.Web.Mvc
命名空间。
ViewBag
是动态属性,属于ControllerBase
抽象类。
ControllerBase类实现IController接口并添加几个 方法和属性(例如ViewBag)。它定义了一个摘要 负责定位操作方法的ExecuteCore方法 并执行它。如果您选择从中派生控制器 ControllerBase,您将必须为此提供实现 方法。
Controller类从ControllerBase派生。它提供了 ExecuteCore方法的实现,并添加了几个有用的方法 您可以在控制器中使用(例如View(),Redirect()等)。
总结一下-ControllerBase和Controller都是内置库 控制器的类。内置的,因为它们是 ASP.NET MVC框架。控制器的基类,因为如果 从它们派生出来,您将创建一个控制器。
Above copied from this link to give you more idea
样本
using System.Web.Mvc;
namespace Sample.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public void Upload(string searchString)
{
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.UploadError = "Upload file error";
}
}
}