ASP. NET MVC4 transmits Model to View, mvc4model
Original article published in: http://www.star110.com/Note/ReadArticle? Id = 54 & title = ASP. NET + MVC4 + % E4 % BC % A0 % E9 % 80% 92 Model % E5 % 88% B0View
Development Environment:. NET MVC4 + EF6.0
Model:
| 1234567891011 |
// Note List Data public class NoteData { public int Id { set; get; } public String Author { set; get; } public String title { set; get; } public String Time { set; get; } public int Read { set; get; } public String TypeName { set; get; } public String Url { set; get; } } |
| 12345678910 |
// My note homepage model public class NoteViewModel : BaseLayoutViewModel { // Note list public List<NoteData> Notes { set; get; } public NoteViewModel() { Notes = new List<NoteData>(); } } |
Background code:
| 123456 |
public ActionResult Index(String type = "",int page = 1) { // Get my note view model data NoteViewModel viewmodel= articlefunc.GetViewModel(type,page); return View("Index", viewmodel); } |
Front-end code:
Note: You must declare the model type in the first line of the View File.
@ Model NoteViewModel
| 123456789101112131415161718192021222324 |
<table class="table table-striped" id="dataTable" style="width:100%;"> <thead> <tr> <th> Category </th> <th style="text-align:center"> Title </th> <th> Author </th> <th> Read </th> </tr> </thead> <tbody> @foreach (var note in Model.Notes) { <tr> <td>@note.TypeName</td> <td><i class="fa fa-columns"> <a href="@note.Url" target="_blank"> @note.title</a> </i> </td> <td><i class="fa fa-user"> @note.Author</i></td> <td>@note.Read</td> </tr> } </tbody> </table> |