ASP. NET MVC4 transmits Model to View, mvc4model

Source: Internet
Author: User

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 { 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; }    }

 

12345678910 // My note homepage model    public class NoteViewModel : BaseLayoutViewModel    {        // Note list        public List<NoteData> Notes { setget; }        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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.