Hi I'm reading a book called "ASP.NET MVC 4 in Action",
on page 84, the author is introducing Model–view–viewmodel and is saying that "one problem with MVC approach is that it makes the view quite complex.
To make the view as maintainable as possible, it should be as dumb as possible—complex looping and calculation logic should be performed at a
higher level...
Data can be manipulated in the model level, in the database / query level, or in controllers. But things like looping through a list of items to display them in a View, well, that makes sense in the View. That is not complex. But if you are reading a
book that makes that assertion, surely it has examples of what it considers complex and what the solutions might be?
An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic, validation logic, and database access logic. For example, if you are using the Microsoft
Entity Framework to access your database, then you would create your Entity Framework classes (your .edmx file) in the Models folder.
A view should contain only logic related to generating the user interface. A controller should only contain the bare minimum of logic required to return the right view or redirect the user to another action (flow control). Everything else should be contained
in the model.
In general, you should strive for fat models and skinny controllers. Your controller methods should contain only a few lines of code. If a controller action gets too fat, then you should consider moving the logic out to a new class in the Models folder.
<end>
If you find the post has answered your issue, then please mark post as 'answered'.
Member
39 Points
63 Posts
How to make maintainable View - Model View View-Model
Jan 31, 2020 05:54 AM|LetMeCode|LINK
Hi I'm reading a book called "ASP.NET MVC 4 in Action",
on page 84, the author is introducing Model–view–viewmodel and is saying that "one problem with MVC approach is that it makes the view quite complex.
To make the view as maintainable as possible, it should be as dumb as possible—complex looping and calculation logic should be performed at a higher level...
So my question is that : What is High level ?
can you also give example / sample code ?
Contributor
6051 Points
2515 Posts
Re: How to make maintainable View - Model View View-Model
Jan 31, 2020 06:04 AM|KathyW|LINK
Data can be manipulated in the model level, in the database / query level, or in controllers. But things like looping through a list of items to display them in a View, well, that makes sense in the View. That is not complex. But if you are reading a book that makes that assertion, surely it has examples of what it considers complex and what the solutions might be?
All-Star
194855 Points
28100 Posts
Moderator
Re: How to make maintainable View - Model View View-Model
Jan 31, 2020 07:56 AM|Mikesdotnetting|LINK
A relatively simple and common example - you might have a view where certain parts are displayed based on complex conditions e.g:
@if((Model.MyBool && Model.SomeInt = 33 && Model.AnotherBool == false) || Model.SomeOtherCondition){
// show some code
}
You can move those conditions to another property on the model so that the conditional logic is hidden from the view:
public bool ShowThis => Model.MyBool && Model.SomeInt = 33 && Model.AnotherBool == false) || Model.SomeOtherCondition;
Then in the view, you simply replace the above with
@if(Model.Showthis){
// show some code
Other "higher levels" include custom HTML helpers: https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/views/creating-custom-html-helpers-cs and Razor Helper functions: https://www.mikesdotnetting.com/article/173/the-difference-between-helpers-and-functions-in-webmatrix
Contributor
4973 Points
4262 Posts
Re: How to make maintainable View - Model View View-Model
Jan 31, 2020 08:09 AM|DA924|LINK
So my question is that : What is High level ?
https://www.c-sharpcorner.com/UploadFile/56fb14/understanding-separation-of-concern-and-Asp-Net-mvc/
The higher level is classes/objects in the Models folder.
https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/overview/understanding-models-views-and-controllers-cs
<copied>
An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic, validation logic, and database access logic. For example, if you are using the Microsoft Entity Framework to access your database, then you would create your Entity Framework classes (your .edmx file) in the Models folder.
A view should contain only logic related to generating the user interface. A controller should only contain the bare minimum of logic required to return the right view or redirect the user to another action (flow control). Everything else should be contained in the model.
In general, you should strive for fat models and skinny controllers. Your controller methods should contain only a few lines of code. If a controller action gets too fat, then you should consider moving the logic out to a new class in the Models folder.
<end>