ASP.NET MVC4 傳遞Model到View


原文發表在:http://www.star110.com/Note/ReadArticle/60641215331146140043.html

開發環境:.NET MVC4 + EF6.0

模型:

1
2
3
4
5
6
7
8
9
10
11
//Note列表數據
     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 ; }
     }

 

1
2
3
4
5
6
7
8
9
10
//我的筆記首頁model
     public  class  NoteViewModel : BaseLayoutViewModel
     {
         //筆記列表
         public  List<NoteData> Notes {  set get ; }
         public  NoteViewModel()
         {
             Notes =  new  List<NoteData>();
         }
     }

 

 

后台代碼:

1
2
3
4
5
6
public  ActionResult Index(String type =  "" , int  page = 1)
         {
             //得到我的筆記視圖模型數據
             NoteViewModel viewmodel= articlefunc.GetViewModel(type,page);
             return  View( "Index" , viewmodel);
         }

前端代碼:

注意:要在視圖文件第一行聲明model類型

@model NoteViewModel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
< table  class = "table table-striped"  id = "dataTable"  style = "width:100%;" >
                     < thead >
                         < tr >
                             < th >分類</ th >
                             < th  style = "text-align:center" >標題</ th >
                             < th >作者</ th >
                             < th >閱讀</ 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 >


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM