Bir*_*Bir 9 c# asp.net asp.net-mvc asp.net-mvc-4
我是ASP.NET MVC4的新手.我正在阅读本教程http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-view.我不清楚返回View()方法.要将数据从视图发送到控制器,我已使用此代码
public ActionResult Index()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
这里返回View()方法将数据从视图返回到控制器.要从控制器发送数据到视图,我已经使用了这段代码
public ActionResult Welcome(string name, int numTimes = 1)
{
ViewBag.Message = "Hello " + name;
ViewBag.NumTimes = numTimes;
return View();
}
Run Code Online (Sandbox Code Playgroud)
这是我的困惑点.返回View()方法是否将ViewBag.Message和ViewBag.NumTimes返回到Welcome视图.或欢迎视图的值返回到欢迎方法?
请帮我清除这一部分.
你很困惑。您正在将值发送ViewBag到欢迎视图。一旦视图处理完成,您就可以将其返回给调用此操作的人。
线条
ViewBag.Message = "Hello " + name;
ViewBag.NumTimes = numTimes;
Run Code Online (Sandbox Code Playgroud)
设置ViewBag欢迎视图使用的值。
进而
return View();
Run Code Online (Sandbox Code Playgroud)
将 Welocme 视图返回给请求的用户Welcome Action.
return View()基本上是类内的一个函数Controller,它返回一个实例ViewResult
它像是
ViewResult view=new ViewResult();
return view;
Run Code Online (Sandbox Code Playgroud)
或者简单地
return View()
Run Code Online (Sandbox Code Playgroud)
有关 ViewBag 的更多信息
ASP.NET MVC 中的 ViewBag 是如何工作的