MVC: pass model / model data to view from controller?

If the view should receive data from the model, do you think the controller should:

a) transfer the model to the representation
b) transfer the model data to the representation
c) none; this should not be a controller issue. Allow the view to access the model directly to retrieve data. Just let the controller provide some parameters that need to be displayed to filter data from the model.
d) it depends on the situation. e) none of the above, but [...]

thanks

After some discussion in the comments on the answer that was deleted by the user, perhaps this needs clarification. My understanding of the MCV architecture is biased towards the Zend Framework (php) architecture, in which the action in the controller has a default assignment assigned to it. So this is not so much a model that dictates which point of view corresponds, but rather a controller. Do you think the model should dictate which point of view is appropriate? The only way I can build a view based on a model is to let the controller pass the model to the view. Are there other methods that allow the representation of access to a model without a controller? Or is it great to allow the controller to pass the model into a view,so that the view can be built on the basis of model attributes?

+5
source share
7 answers

e) None of the above; go to the optimized for viewing "ViewModel".

Example in ASP.NET MVC: -

public ActionResult Details(int id)
{
  Product p = ProductService.GetProductById(id);

  if(p == null) { return RedirectToAction("Index"); }

  ProductViewModel model = new ProductViewModel(p);
  return View(model);
}

both a) and b) “will do” in accordance with d). Never c).

Typically, a ViewModel simply encapsulates a model (if nothing complicated happens, your view can access the model directly through ProductViewModel.Product). If, however, the view needs to do something complex with the model, the responsibility for this should rest with the ViewModel, and not with the responsibility of the Controller or View.

. , ( , ), , , . . , . (, , , , ..), ViewModels, , , eachother. :)

: -

http://www.thoughtclusters.com/2007/12/datamodel-and-viewmodel/

http://stephenwalther.com/blog/archive/2009/04/13/asp.net-mvc-tip-50-ndash-create-view-models.aspx

http://www.nikhilk.net/Silverlight-ViewModel-MVC.aspx

, ViewModels - .NET, , PHP.

, .

+10

, " ", - , , .

" " . , ( , :-), , .

, , , - (), - , ()

+2

, , , . ) - - .

:

e) ; "ViewModel".

( " " ) . , ( , ).

"ViewModel" - "getXXX" ViewModel, , , .

, , ( DRY - ) , , ( , ).

+2

a)

. , "", ". b) MVC. , - . , , . , ? -? , - .

+1

).

, . , , - viewHelper . API, , . , "" . viewHelper.

:

class Post {
    public function export(ViewHelper $helper) {} // I try to avoid getters when I can
}

class PostViewHelper {
    public function getTitle($parameters) {} // title of current post in the loop
}

class PostView {
    private $helpers = array();
    public function loadTemplate($path) {}
    public function addHelper(ViewHelper $helper, $name) {}
    public function __get($key) {} // if exists $this->helper[$key] etc
}

<h1><?php $this->post->getTitle(); ?></h1>

, -. , , viewHelper, API- view/template.

+1

, . a b.

- . , . . MVC .

(a) . . Foo, Foo.

(b) - - value/DTO, ( ViewModel ). , , . 1 000 000 , ; 1000 000 .

And the exact implementation really depends on the language and structure you use. But I think these recommendations are a good start.

+1
source

uhh b.

I really do not see the difference between a and b, and then with some technique of how you will transmit the data.

usually you pass a data map to a view with some data from the model

0
source

All Articles