zoukankan      html  css  js  c++  java
  • 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 { setget; }
            public String Author { setget; }
            public String title { setget; }
            public String Time { setget; }
            public int Read { setget; }
            public String TypeName { setget; }
            public String Url { setget; }
        }

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    //我的笔记首页model
        public class NoteViewModel : BaseLayoutViewModel
        {
            //笔记列表
            public List<NoteData> Notes { setget; }
            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="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>
  • 相关阅读:
    莎士比亚的对白
    拟人、比喻及修辞 —— 生动的英语
    拟人、比喻及修辞 —— 生动的英语
    32位与64位、单精度(single-precision)与双精度(double-precision)
    32位与64位、单精度(single-precision)与双精度(double-precision)
    OpenGL(十七) 绘制折线图、柱状图、饼图
    计算机的组成 —— PCI(PCIE)、PCB
    计算机的组成 —— PCI(PCIE)、PCB
    ios 打电话结束返回到应用中
    paip.输入法编程----删除双字词简拼
  • 原文地址:https://www.cnblogs.com/zhuxiaoxiao/p/5548919.html
Copyright © 2011-2022 走看看