ASP.NET MVC4 传递Model到View
生活随笔
收集整理的這篇文章主要介紹了
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> |
轉載于:https://www.cnblogs.com/zhuxiaoxiao/p/5548919.html
總結
以上是生活随笔為你收集整理的ASP.NET MVC4 传递Model到View的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: React 移动 web 极致优化
- 下一篇: 验证状态、禁用