{
// GET: First
public ActionResult Index()
{
base.ViewData["Something"] = 12345;
return View();
}
}
这是最简单的。
return View 是Conreoller的方法,有这么多重载
protected internal ViewResult View(string viewName, object model);
protected internal ViewResult View(string viewName, string masterName);
protected internal ViewResult View(object model);
protected internal virtual ViewResult View(string viewName, string masterName, object model);
protected internal ViewResult View(string viewName);
我们写的ViewData["Something"] = 12345; 这个ViewData也是Controller的,所以对ViewData赋值,会传进Controller,也就是会被View()接收到。
public abstract class ViewResultBase : ActionResult
{
public object Model
{
get
{
return this.ViewData.Model;
}
}
}
这几句话,是说为什么@model 可以起作用。
public TempDataDictionary TempData
{
get
{
if (this._tempData == null)
{
this._tempData = new TempDataDictionary();
}
return this._tempData;
}
set
{
this._tempData = value;
}
}
下面直接上代码
[Dynamic]
public dynamic ViewBag
{
[return: Dynamic]
get
{
if (this._dynamicViewData == null)
{
this._dynamicViewData = new DynamicViewDataDictionary(() => this.ViewData);
}
return this._dynamicViewData;
}
}
public ViewDataDictionary ViewData
{
get
{
if (this._viewData == null)
{
this._viewData = new ViewDataDictionary();
}
return this._viewData;
}
set
{
this._viewData = value;
}
}
这几句说明 this._tempData的原理,
public abstract class ViewResultBase : ActionResult