Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 794
Bir Avatar asked Mar 03 '14 08:03

Bir


People also ask

What does return View () do in C#?

View discovery The default behavior of the View method ( return View(); ) is to return a view with the same name as the action method from which it's called. For example, the About ActionResult method name of the controller is used to search for a view file named About. cshtml .

Can I return action result instead of view result?

When you set Action's return type ActionResult , you can return any subtype of it e.g Json,PartialView,View,RedirectToAction.

What is the meaning of return view?

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


3 Answers

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.

like image 66
jarnail Avatar answered Oct 12 '22 22:10

jarnail


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

like image 35
शेखर Avatar answered Oct 13 '22 00:10

शेखर


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

like image 30
adhie Avatar answered Oct 13 '22 00:10

adhie