简体   繁体   English

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

[英]Confuse about return View() method in ASP.NET MVC4

I am new in ASP.NET MVC4 . 我是ASP.NET MVC4的新手。 I am reading this tutorial http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-view . 我正在阅读本教程http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-view I am not clear about return View() method. 我不清楚返回View()方法。 To send data from view to controller , i have used this code 要将数据从视图发送到控制器,我已使用此代码

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

here the return View() method return data from view to controller. 这里返回View()方法将数据从视图返回到控制器。 To send data from controller to view , i have used this code 要从控制器发送数据到视图,我已经使用了这段代码

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. 返回View()方法是否将ViewBag.Message和ViewBag.NumTimes返回到Welcome视图。 OR the value of Welcome view return to the Welcome method? 或欢迎视图的值返回到欢迎方法?

Please help me to clear this part. 请帮我清除这一部分。

You are quite confused. 你很困惑。 You are sending values through ViewBag to the Welcome view. 您正通过ViewBag将值发送到欢迎视图。 And once processing of view is done you are retuning it to one who has called this action. 一旦处理完视图,您就会将其重新调整为已调用此操作的人。

The lines 线条

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

Setting the ViewBag value which is used by the Welcome view. 设置Welcome视图使用的ViewBag值。
And then 然后

 return View();

Will return the Welocme View to the user who has requested for Welcome Action. 将Welocme视图返回给已请求Welcome Action.的用户Action.

Edit 1 编辑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 有关ViewBag的更多信息
How ViewBag in ASP.NET MVC works ASP.NET MVC中的ViewBag如何工作

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);
    }
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). 此函数签名表示“action”方法返回一些结果(如您可以看到返回类型ActionResult)。 This is an abstract class telling ASP.NET MVC how to write that result to the Response. 这是一个抽象类,告诉ASP.NET MVC如何将该结果写入Response。 You should explore the different types of action results: 您应该探索不同类型的操作结果:

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. 这些不同类型的动作结果是ActionResult的子类,如HttpStatusCodeResult,JsonResult或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(); 如果你返回View(),你只需返回一个告诉ASP.NET MVC它应该呈现cshtml页面的对象,如果你返回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: 例如,浏览器将获得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);.

The statement return View() does not return ViewBag. 语句return View()不返回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") 默认情况下,它返回一个与您的操作名称同名的视图,或者如果您明确提供视图名称(如return View("myView") ,则返回自定义视图,如return View("myView")

On the other hand ViewBag is simply a way to pass data from your controller to your view. 另一方面,ViewBag只是​​一种将数据从控制器传递到视图的方法。

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 ,您现在可以将ViewBag.MessageViewBag.NumTimes的值用于View。

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 ,该View可以在HTML中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM