2

I am moving to MVC after years of using WebForms and I have a question about complex pages and how MVC works

I understand the controller creates a model which is passed to a view which is then displayed to the user.

If we take the example Microsoft homepage as an example (Shown at the bottom of this post) and pretend that each of the areas highlighted in yellow are separate database driven areas that are combined on the homepage and extracted by a call to the home controller / index method.

My question is about how a page such as the one presented below would construct the necessary model then pass it back to the view for rendering.

My first option would be to created complex ViewModel which contains each of the subsections (and each sub-section content) and pass that back to the view but I would think on complex pages this would get messy quite quickly.

Another option might be to use a bunch of RenderAction calls (I'm unsure if this is even correct) at each place where a subsection appears.

Perhaps PartialViews / ViewComponent may be another option available to me.

In the WebForm world I would have used UserControls each with their own logic in the code-behind page but obviously I cannot do this here (Although ViewComponents do seem similar)

So, given a complex page what is the best practice to generate a model or models and view / views in order to keep everything nice and clean.

Thanks for your advice :)

enter image description here

1
  • All of the options you mentioned are suitable but which is best depends on multiple factors Commented Aug 9, 2017 at 5:39

1 Answer 1

1

This data is display only. Bring data in one complex model and pass to other partial view from that.

You can also load main data (jumbotron) at page load, then load rest through ajax jquery calls. When you have to submit form data to server, handle that separately.

Complex models won't get messy here because they have different display only roles. You can create models for different partial views and put them in main model as properties of that class. Then, pass these properties to partial views for rendering.

Again, handle post data separately. Post data should be minimal and do not post the entire main page model for few fields of post data. Use jQuery ajax for that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.