Simple Best Practices for ASP.NET Models

I want my view to look like this:

@model MyProgram.Models.DocumentList @{ ViewBag.Title = "View1"; } @foreach (MyProgram.Models.Document doc in Model.GetDocs()) { <p>doc.Content</p> } 

This makes me think that I need a model containing a list of another model. Therefore, in a project containing nothing but this, I would:

 /Model/DocumentList.cs /Model/Document.cs 

Is this right or is there a better way?

+7
c # asp.net-mvc asp.net-mvc-4
source share
4 answers

Use View Models so that you can transfer as many models as you need. I also use presentation models to check POST validation, etc. So in your case, you can do something like:

View model

 public class MyViewModel { public string PageTitle { get; set; } public DocumentList DocList { get; set; } // You could use your DocumentList object here, or if you // don't want to, just pass a List<T> of Document public List<Document> DocList { get; set; } } 

Your kind

 @model MyViewModel @{ ViewBag.Title = Model.PageTitle; } @foreach (var doc in Model.DocList) { <p>doc.Content</p> } 

There MUCH is more for MVC than this, although, for example, Display and Editor Templates , so I would look on the Internet for some good guides that cover the main features of MVC.

Edit

To be fair, after reading this, you are already following this principle, so the DocumentList is your look model (and you really don't need the GetDocs method, just have a collection property if you are not following the logic in the collection before returning this message).

Hope this answer helps you clarify a few things?

+4
source share

It depends on your needs. If you only need a list of document models in your view, I would only go with the Document model and change the view model passed to:

 @model List<MyProgram.Models.Document> @{ ViewBag.Title = "View1"; } @foreach (MyProgram.Models.Document doc in Model) { <p>doc.Content</p> } 

But if you need more properties or methods in your view, I would go with a view model containing List<MyProgram.Models.Document> and other properties.

 @model DocumentsViewModel @{ ViewBag.Title = "View1"; } @foreach (MyProgram.Models.Document doc in Model.DocumentList) { <p>doc.Content</p> } 

Model:

 public class DocumentsViewModel { public List<MyProgram.Models.Document> DocumentList {get; set;} ... other properties } 
+2
source share

You are right in considering that for this you need 2 model classes.
I would use the List property, so your code would look like this:

 @foreach (var doc in Model.Docs) 

Also: Standard mvc creates a model folder for each controller.
Thus, by default, the mvc project looks

 root Controllers YourFirstController Views YourFirst Index Models YourFirst DocumentList Document 

* update: If you only need a list of items, you can also pass a list of your model as follows:

 @model List<MyProgram.Models.Document> @{ ViewBag.Title = "View1"; } @foreach (var doc in Model) { <p>doc.Content</p> } 
+1
source share

In such cases, there is a logical concept. This is called a ViewModel. A view model class can contain several model objects and other properties depending on the view. ViewModel must have all the data necessary for the presentation. So, in your case, create a ViewModel named DocumentViewModel and create objects of two other models.

Hope this helps.

+1
source share

All Articles