Output HTML using the Mustache.js template engine

Source: Internet
Author: User

MVC Model

  Public classStudentmodel {[Key] Public intStuid {Get;Set; } [Display (Name="name")] [Stringlength ( -)] [Required (ErrorMessage="name must be filled in")]         Public stringStuname {Get;Set; } [Stringlength ( -)] [Display (Name="Address")] [DataType (datatype.multilinetext)]//[Required (errormessage = "address Required")]         Public stringAddress {Get;Set; } [Display (Name="Age")] [Required (ErrorMessage="age must be filled")] [DisplayFormat (Applyformatineditmode=true, dataformatstring ="{0:C}")]         Public intAge {Get;Set; } }
Studentmodel

Mvc5webcontext

  Public classMvc5webcontext:dbcontext {//You can add the custom code to the this file.        Changes is not being overwritten. //         //If you want the Entity Framework to drop and regenerate your database//automatically whenever the your model schema, use data migrations. //For more information refer to the documentation:// http://msdn.microsoft.com/en-us/data/jj591621.aspx             PublicMvc5webcontext ():Base("Name=mvc5webcontext")        {        }         PublicSystem.data.entity.dbset<mvc5web.models.studentmodel> Studentmodels {Get;Set; } }
Mvc5webcontext

Controller

 Public class Studentcontroller:controller    { public jsonresult jsonlist ()        {            //Studentmodel stu = new Studentmodel { Stuid = Stuname = "wilson100", age = +, Address = "HG"};            return Json (db. Studentmodels, Jsonrequestbehavior.allowget);}        }
View Code

HTML templates

<script id="Template"Type="text/html"> <table border="1"> <tr> <th> name </th> <th> address </th> <th> age </t H> </tr>{{#studentLists}}<tr> <td>{{Stuname}}</td> <td>{{Address}}</td> <td>{{Age}}</td> </tr>        {{/Studentlists}} </table></script>
View Code

Ajax requests for client template output

  $ ("#btnTest"). Click (function  () {                $.get (  data) {                    var Template = $ (' #template '). html ();                    Mustache.parse (template);    // optional, speeds up the future uses                    var rendered = Mustache.render (template, {studentlists:data});                    $ (' #target '). html (rendered);                }             );
View Code

Mustache.js Official information

http://mustache.github.io/

Https://github.com/janl/mustache.js

Output HTML using the Mustache.js template engine

Related Article

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.