gpt4 book ai didi

c# - 对 ASP.NET MVC4 中的 return View() 方法感到困惑

转载 作者:太空狗 更新时间:2023-10-29 17:43:19 26 4
gpt4 key购买 nike

我是 ASP.NET MVC4 的新手。我正在阅读本教程 http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-view .我不清楚 return View() 方法。为了将数据从 View 发送到 Controller ,我使用了这段代码

 public ActionResult Index()
{
return View();
}

这里的 return View() 方法将数据从 View 返回到 Controller 。为了从 Controller 发送数据到 View ,我使用了这段代码

public ActionResult Welcome(string name, int numTimes = 1)
{
ViewBag.Message = "Hello " + name;
ViewBag.NumTimes = numTimes;
return View();

}

这是我的困惑点。 return View() 方法是否将 ViewBag.Message 和 ViewBag.NumTimes 返回到欢迎 View 。或者 Welcome View 的值返回到 Welcome 方法?

请帮我清除这部分。

最佳答案

你很困惑。您正在通过 ViewBag 将值发送到 Welcome View 。一旦 View 处理完成,您将把它返回给调用此操作的人。

线条

 ViewBag.Message = "Hello " + name;
ViewBag.NumTimes = numTimes;

设置 Welcome View 使用的 ViewBag 值。
然后

 return View();

将 Welocme View 返回给请求 Welcome Action 的用户。

编辑1

return View() 基本上是 Controller 类中的一个函数,它返回一个 ViewResult

的实例

好像是

 ViewResult view=new ViewResult();
return view;

或者只是

 return View()

关于 ViewBag 的更多信息
How ViewBag in ASP.NET MVC works

关于c# - 对 ASP.NET MVC4 中的 return View() 方法感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22142244/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com