1110
1110

Reputation: 6829

Need advice on how to structure asp mvc razor page

enter image description here I have one page that will contain a lot data. Outline of the page is on the image. For output I have used "razor" engine.

I have made one "ViewModel" class that contains all data I need and set that viewModel class as razor page model. Is this the right way how I should do this or there is some smarter way?

public class MyViewModelClass{
    public ValueObjectx x;
    public ValueObjecty y;
    public ValueObjectz z;

    public List<ValueObjectT> tList;
    public List<ValueObjectG> gList;
    public List<ValueObjectS> sList;
}

Upvotes: 1

Views: 193

Answers (2)

Tomas Jansson
Tomas Jansson

Reputation: 23462

I would not use one ViewModel for all the data. I would only use the ViewModel for the data that might actually be posted back to the server. For all the other data, like lists, I would put it on the ViewBag. Like:

public ViewResult SomeAction()
{
    var model = GetYourModelData();
    ViewBag.TList = GetTList();
    ViewBag.GList = GetGList();
    ViewBag.SList = SList();
    return View(model);
}

You could put all the lists in a separate helper object if you use them a lot.

Note that this is not a "this is how you should do it answer", it is more a description of what works for me. But as I see it the model of the view is one thing and the actual data, as data in drop downs etc., on the view is another thing and should be put in the ViewData property, but the ViewBag is just a dynamic wrapper around the ViewData property. You can then use the properties on the ViewBag as input to your partial views that should make up each of your sections.

Upvotes: 1

Timothy Strimple
Timothy Strimple

Reputation: 23060

In a scenario like this I would typically have each section as a partial view. Each section is responsible for it's own layout and ViewModel. This prevents the tendency to have a super view which has to know about too many things in order to render.

Upvotes: 4

Related Questions