简体   繁体   中英

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

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

 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

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?

Please help me to clear this part.

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.

The lines

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

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

 return View();

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

Edit 1

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

It is like

 ViewResult view=new ViewResult();
 return view;

or simply

 return View()

More about ViewBag
How ViewBag in ASP.NET MVC works

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:

 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):

 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). 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:

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:

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

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);.

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")

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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