2

I've been reading too many tutorials already and none of it explain what this means in an action result

return View(model);

Why can't we simple return view? Why do we put a model parameter in our action result's return view()?

Also, anyone can give an example for a situation where I have to use return view(model)?

anaval
  • 1,130
  • 10
  • 24
  • 1
    Because view uses model data to render HTML code. – Dennis Dec 15 '15 at 06:06
  • You don' have to, but if you want the view to use the values of your model properties, then you must return the model to the view (other wise the default values will be used) –  Dec 15 '15 at 06:07
  • MVC is Model View Controller pattern. You need pass `Model` from `Controller` to `View`. Sometimes no need to pass `Model` to view. But mostly you need pass – ebattulga Dec 15 '15 at 06:11

7 Answers7

8

Because your view uses the model to build its content.

If you had a model like this:

public class Order
{
    public decimal Total { get; set; }
}

And your controller:

public ActionResult GetOrderInformation(int orderID)
{
    var order = LoadOrder(orderID);
    return View(order);
}

With this view:

@model Order
<html>
    <head> </head>
    <body>
        Your order costs @Model.Total
    </body>
</html>

You can see how the view is completely independent from the controller, or how the information is retrieved - which is the whole idea behind MVC. The view only knows about the model, and how to display the model - nothing else.

The controller knows who to ask for the data, but not how to display it

Rob
  • 26,989
  • 16
  • 82
  • 98
4

You don't need to.

If the view you are rendering is static with no info needed from the controller then you can just end your Action with a return View();

public ActionResult SomeAction()
{
    ...
    return View();
}

Then in your .cshtml file don't declare any @model directive and it'll be fine.

Carlos Muñoz
  • 17,397
  • 7
  • 55
  • 80
1

You can return simply view, but when you pass the model into view your can use it as you viewbag, and by that bind view elements with the model data.

Pavel Durov
  • 1,287
  • 2
  • 13
  • 28
  • do you have any example code? I'm having a hard time grasping this part in learning mvc – anaval Dec 15 '15 at 06:12
  • View function is a regular function which returns ActionResult object and it has several overloads, one of them doesn't accepts object as a parameter. You can simply go to the function definition and explore all the possible function implementations. :) – Pavel Durov Dec 15 '15 at 07:35
  • https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.view(v=vs.118).aspx – Pavel Durov Dec 15 '15 at 08:41
1

You can simply use

return View();

but if you need to bind data then you have to pass like

return View(model);

for an example Model Class

public class BookViewModel
    {
        public Guid Id { get; set; }
        public string Name { get; set; }         
    }

Controller Method

public ActionResult Index()
{
    return View();
}
public ActionResult Create()
{
    return View(new BookViewModel());
}

View

  @using (Html.BeginForm())
    {
        @Html.HiddenFor(m => m.Id)
        <div class="editor-label">
            @Html.LabelFor(m => m.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(m => m.Name)
            @Html.ValidationMessageFor(m => m.Name)
        </div>
    }
DevT
  • 4,843
  • 16
  • 59
  • 92
1

You don't need to pass objects to an action result, although if you don't you can't access the model.

i.e. It is common to do this on static pages such as About screens:

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

Also, it is possible to pass other objects instead of the full model. See post: Must ASP.NET MVC Controller Methods Return ActionResult?

Community
  • 1
  • 1
Shep
  • 638
  • 3
  • 15
1

In Asp.net MVC, View(chtml) is for redering UI; if your View is simple or static, you can construct your html content in the View without external data.

If you need data (from Db, web service, etc), your View must be able to query the data source.

There are several ways to pass data to view; via the View(Model) statement, via viewbag, etc.

we can also pass service layer object so view can query the datasource by itself.

Why do we need to pass Model to View again? Once in View (cshtml) you cannot access Controller -> you can pass ready to read data or service layer object inside your Model

kite
  • 1,478
  • 1
  • 15
  • 27
  • "we can also pass service layer object so view can query the datasource by itself." - Best practices would say to go against this approach. The view should be able to display data in the model, not how to retrieve the data. It is the controllers responsibility to determine how to define the data for the view. – Steven Anderson Dec 15 '15 at 06:40
  • sometimes UI component do need to access service layer. eg. Grid component doing server side paging, sorting, grouping (million records) – kite Dec 15 '15 at 07:13
  • For example look at how DevExpress GridView component handle large database; They stored IQueryable in the MVC model, so their grid can handle paging, sorting, filtering, etc. https://documentation.devexpress.com/AspNet/14760/ASP-NET-MVC-Extensions/Grid-View/Concepts/Binding-to-Data/Binding-to-Large-Data-Database-Server-Mode – kite Aug 04 '17 at 15:17
1

You can just return a view:

View()

In that view, you won't have a @model you can take data of. You can still pass ViewBag or ViewData to send some dynamic data to the view.

Is it possible to have a View without a model?

YES

Some cases:

  • Semi static page: You don't need a model because almost everything that will be displayed in the page is static.

  • Container page: It can contain a lot of partials views, javascript calls or whatever you can imagine to retrieve data dinamically.

  • Many other cases in which the existence of a Model doesn't correspond to a business logic (or a good practice logic)

Just remember that patrons are there to guide us, not to force us to do things just one way. Check what's best for your application, change the bits that you don't like. Try to take the best of the framework, but remember it is there to help you.

Lucas Rodriguez
  • 1,203
  • 6
  • 15