混淆ASP.NET MVC4中的返回View()方法

时间:2021-06-13 11:21:39

I am new in ASP.NET MVC4 . I am reading this tutorial http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-view . I am not clear about return View() method. To send data from view to controller , i have used this code

我是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();
    }

here the return View() method return data from view to controller. To send data from controller to view , i have used this code

这里返回View()方法将数据从视图返回到控制器。要从控制器发送数据到视图,我已经使用了这段代码

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

    }

Here is my confusing point. Is the return View() method return ViewBag.Message and ViewBag.NumTimes to the Welcome view. OR the value of Welcome view return to the Welcome method?

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

Please help me to clear this part.

请帮我清除这一部分。

6 个解决方案

#1


3  

You are quite confused. You are sending values through ViewBag to the Welcome view. And once processing of view is done you are retuning it to one who has called this action.

你很困惑。您正通过ViewBag将值发送到欢迎视图。一旦完成视图处理,您就会将其重新调整为已调用此操作的人。

The lines

线条

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

Setting the ViewBag value which is used by the Welcome view.
And then

设置Welcome视图使用的ViewBag值。接着

 return View();

Will return the Welocme View to the user who has requested for Welcome Action.

将Welocme视图返回给已请求欢迎操作的用户。

Edit 1

return View() is basically a function inside the Controller class which returns an instance of ViewResult

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

It is like

它像是

 ViewResult view=new ViewResult();
 return view;

or simply

或简单地说

 return View()

More about ViewBag
How ViewBag in ASP.NET MVC works

有关ViewBag的更多信息ASP.NET MVC中ViewBag的工作原理

#2


3  

In asp.net MVC if you want to send data from action to view, you can send a type of data or viewbag/viewdata that you have used in your sample, take a look:

在asp.net MVC中,如果要将数据从操作发送到视图,您可以发送您在样本中使用的一种数据或viewbag / viewdata,看看:

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

you can have something like this in your view:

您可以在视图中看到类似的内容:

@ViewBag.Message 
@ViewBag.NumTimes 

but, ViewBag and ViewData is just for sending information from action to view, and for sending view to action you should pass a type(Model or viewModel):

但是,ViewBag和ViewData仅用于从操作向视图发送信息,并且为了将视图发送到操作,您应该传递类型(Model或viewModel):

 public ActionResult Welcome(string name, int numTimes = 1)
    {
      var model = new List<ModelClass>
            {
                new ModelClass{name="name",numTimes=1}
            };
        return View(model);
    }

so in your view you should bind the model:

所以在你的视图中你应该绑定模型:

@model List<ModelClass>
@foreach(var item in Model)
{
     // show you items
}

and finally when you want to pass the model to action, you have to act like this:

最后当你想要将模型传递给动作时,你必须这样做:

[httpPost]
 public ActionResult Welcome(ModelClass modelClass)
    {
       if(ModelState.isValid)
       {
             // operation on your data 
       }
        return View(model);
    }

#3


3  

public ActionResult Welcome(string name, int numTimes = 1)

This function signature indicates that an "action" method is returning some result (as you can see the return type ActionResult). This is an abstract class telling ASP.NET MVC how to write that result to the Response. You should explore the different types of action results:

此函数签名表示“action”方法返回一些结果(如您可以看到返回类型ActionResult)。这是一个抽象类,告诉ASP.NET MVC如何将该结果写入Response。您应该探索不同类型的操作结果:

http://msdn.microsoft.com/en-us/library/system.web.mvc.actionresult(v=vs.118).aspx

http://msdn.microsoft.com/en-us/library/system.web.mvc.actionresult(v=vs.118).aspx

These different kinds of action results are the subclasses of ActionResult like HttpStatusCodeResult, JsonResult or RedirectResult. If you return View(), you just return an object that tells ASP.NET MVC that it should render the cshtml page, if you return HttpNotFound(); for example, the browser will get 404. You can try several kinds of return values by returning method results of System.Web.Mvc.Controller, for example:

这些不同类型的动作结果是ActionResult的子类,如HttpStatusCodeResult,JsonResult或RedirectResult。如果返回View(),则只返回一个告诉ASP.NET MVC它应该呈现cshtml页面的对象,如果你返回HttpNotFound();例如,浏览器将获得404.您可以通过返回System.Web.Mvc.Controller的方法结果来尝试几种返回值,例如:

return View("OtherViewName"); // If you have an OtherViewName.cshtml file
return RedirectToAction("OtherAction"); // If you have an action called OtherAction
return HttpNotFound();
return new HttpStatusCodeResult(500);
// if you are familiar with JSON:
return Json(1, JsonRequestBehavior.AllowGet);
return Json(new int[2] {1, 2}, JsonRequestBehavior.AllowGet);
return Json(new {A="A", B=123}, JsonRequestBehavior.AllowGet);

Or if you are looking for a more detailed explanation check this page:

或者,如果您正在寻找更详细的解释,请查看此页面:

http://msdn.microsoft.com/en-us/library/dd410269(v=vs.100).aspx

http://msdn.microsoft.com/en-us/library/dd410269(v=vs.100).aspx

These are simply helper functions that will fill the response of the request. Without them, you would have to write Response.OutputStream the content that is parsed from a cshtml file, and filled with the ViewBag's properties, and you even would have to set the http headers like Response.ContentType and Response.AddHeader("Content-Length", 123213);.

这些只是辅助函数,将填充请求的响应。没有它们,您必须编写Response.OutputStream从cshtml文件解析的内容,并填充ViewBag的属性,您甚至必须设置HTTP标头,如Response.ContentType和Response.AddHeader(“Content-长度“,123213);.

#4


1  

return ViewBag.Message and ViewBag.NumTimes to the Welcome view

将ViewBag.Message和ViewBag.NumTimes返回到Welcome视图

Saying this you can now use the values of ViewBag.Message and ViewBag.NumTimes to your View.

说到这里,您现在可以将ViewBag.Message和ViewBag.NumTimes的值用于View。

#5


1  

The statement return View() does not return ViewBag. By default, it returns a View with same name as your action name or returns a custom view say myView.cshtml if you explicitly provide the view name like return View("myView")

语句返回View()不返回ViewBag。默认情况下,它返回一个与您的操作名称同名的视图,或者如果您明确提供视图名称(如返回视图(“myView”)),则返回自定义视图,如myView.cshtml

On the other hand ViewBag is simply a way to pass data from your controller to your view.

另一方面,ViewBag只是​​一种将数据从控制器传递到视图的方法。

#6


1  

return View() will return the View (ie. Welcome.cshtml or Welcome.aspx) for the Action Welcome.

return View()将返回Action Welcome的View(即Welcome.cshtml或Welcome.aspx)。

By setting properties in the Viewbag you can simply pass values to the View which can be used there along your HTML.

通过在Viewbag中设置属性,您只需将值传递给View,该视图可以在HTML中使用。

#1


3  

You are quite confused. You are sending values through ViewBag to the Welcome view. And once processing of view is done you are retuning it to one who has called this action.

你很困惑。您正通过ViewBag将值发送到欢迎视图。一旦完成视图处理,您就会将其重新调整为已调用此操作的人。

The lines

线条

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

Setting the ViewBag value which is used by the Welcome view.
And then

设置Welcome视图使用的ViewBag值。接着

 return View();

Will return the Welocme View to the user who has requested for Welcome Action.

将Welocme视图返回给已请求欢迎操作的用户。

Edit 1

return View() is basically a function inside the Controller class which returns an instance of ViewResult

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

It is like

它像是

 ViewResult view=new ViewResult();
 return view;

or simply

或简单地说

 return View()

More about ViewBag
How ViewBag in ASP.NET MVC works

有关ViewBag的更多信息ASP.NET MVC中ViewBag的工作原理

#2


3  

In asp.net MVC if you want to send data from action to view, you can send a type of data or viewbag/viewdata that you have used in your sample, take a look:

在asp.net MVC中,如果要将数据从操作发送到视图,您可以发送您在样本中使用的一种数据或viewbag / viewdata,看看:

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

you can have something like this in your view:

您可以在视图中看到类似的内容:

@ViewBag.Message 
@ViewBag.NumTimes 

but, ViewBag and ViewData is just for sending information from action to view, and for sending view to action you should pass a type(Model or viewModel):

但是,ViewBag和ViewData仅用于从操作向视图发送信息,并且为了将视图发送到操作,您应该传递类型(Model或viewModel):

 public ActionResult Welcome(string name, int numTimes = 1)
    {
      var model = new List<ModelClass>
            {
                new ModelClass{name="name",numTimes=1}
            };
        return View(model);
    }

so in your view you should bind the model:

所以在你的视图中你应该绑定模型:

@model List<ModelClass>
@foreach(var item in Model)
{
     // show you items
}

and finally when you want to pass the model to action, you have to act like this:

最后当你想要将模型传递给动作时,你必须这样做:

[httpPost]
 public ActionResult Welcome(ModelClass modelClass)
    {
       if(ModelState.isValid)
       {
             // operation on your data 
       }
        return View(model);
    }

#3


3  

public ActionResult Welcome(string name, int numTimes = 1)

This function signature indicates that an "action" method is returning some result (as you can see the return type ActionResult). This is an abstract class telling ASP.NET MVC how to write that result to the Response. You should explore the different types of action results:

此函数签名表示“action”方法返回一些结果(如您可以看到返回类型ActionResult)。这是一个抽象类,告诉ASP.NET MVC如何将该结果写入Response。您应该探索不同类型的操作结果:

http://msdn.microsoft.com/en-us/library/system.web.mvc.actionresult(v=vs.118).aspx

http://msdn.microsoft.com/en-us/library/system.web.mvc.actionresult(v=vs.118).aspx

These different kinds of action results are the subclasses of ActionResult like HttpStatusCodeResult, JsonResult or RedirectResult. If you return View(), you just return an object that tells ASP.NET MVC that it should render the cshtml page, if you return HttpNotFound(); for example, the browser will get 404. You can try several kinds of return values by returning method results of System.Web.Mvc.Controller, for example:

这些不同类型的动作结果是ActionResult的子类,如HttpStatusCodeResult,JsonResult或RedirectResult。如果返回View(),则只返回一个告诉ASP.NET MVC它应该呈现cshtml页面的对象,如果你返回HttpNotFound();例如,浏览器将获得404.您可以通过返回System.Web.Mvc.Controller的方法结果来尝试几种返回值,例如:

return View("OtherViewName"); // If you have an OtherViewName.cshtml file
return RedirectToAction("OtherAction"); // If you have an action called OtherAction
return HttpNotFound();
return new HttpStatusCodeResult(500);
// if you are familiar with JSON:
return Json(1, JsonRequestBehavior.AllowGet);
return Json(new int[2] {1, 2}, JsonRequestBehavior.AllowGet);
return Json(new {A="A", B=123}, JsonRequestBehavior.AllowGet);

Or if you are looking for a more detailed explanation check this page:

或者,如果您正在寻找更详细的解释,请查看此页面:

http://msdn.microsoft.com/en-us/library/dd410269(v=vs.100).aspx

http://msdn.microsoft.com/en-us/library/dd410269(v=vs.100).aspx

These are simply helper functions that will fill the response of the request. Without them, you would have to write Response.OutputStream the content that is parsed from a cshtml file, and filled with the ViewBag's properties, and you even would have to set the http headers like Response.ContentType and Response.AddHeader("Content-Length", 123213);.

这些只是辅助函数,将填充请求的响应。没有它们,您必须编写Response.OutputStream从cshtml文件解析的内容,并填充ViewBag的属性,您甚至必须设置HTTP标头,如Response.ContentType和Response.AddHeader(“Content-长度“,123213);.

#4


1  

return ViewBag.Message and ViewBag.NumTimes to the Welcome view

将ViewBag.Message和ViewBag.NumTimes返回到Welcome视图

Saying this you can now use the values of ViewBag.Message and ViewBag.NumTimes to your View.

说到这里,您现在可以将ViewBag.Message和ViewBag.NumTimes的值用于View。

#5


1  

The statement return View() does not return ViewBag. By default, it returns a View with same name as your action name or returns a custom view say myView.cshtml if you explicitly provide the view name like return View("myView")

语句返回View()不返回ViewBag。默认情况下,它返回一个与您的操作名称同名的视图,或者如果您明确提供视图名称(如返回视图(“myView”)),则返回自定义视图,如myView.cshtml

On the other hand ViewBag is simply a way to pass data from your controller to your view.

另一方面,ViewBag只是​​一种将数据从控制器传递到视图的方法。

#6


1  

return View() will return the View (ie. Welcome.cshtml or Welcome.aspx) for the Action Welcome.

return View()将返回Action Welcome的View(即Welcome.cshtml或Welcome.aspx)。

By setting properties in the Viewbag you can simply pass values to the View which can be used there along your HTML.

通过在Viewbag中设置属性,您只需将值传递给View,该视图可以在HTML中使用。