The advantage of using Mustache is that some html parts that are used repeatedly can be defined as Mustache templates for multiple times. The general steps for using Mustache are as follows:
→ Get json data from the background
→ Get the pre-defined Mustache template on the foreground page (The placeholder must be consistent with the field or attribute name sent from the background)
→ Traverse the json data of each row, and use the Mustache. render (template, row) method to fill the json data into the corresponding placeholder to obtain the html content.
→ Append html content to a location on the page
Show the drop-down list of a football club:
Using System. Web. Mvc;
namespace MvcApplication1.Models.ViewModels
{
public class TeamVm
{
public TeamVm()
{
Players = new List<SelectListItem>();
}
[Display (Name = "club")]
public int ID { get; set; }
public string Name { get; set; }
public IEnumerable<SelectListItem> Players { get; set; }
}
}
Create the ViewModel corresponding to Plyer as PlayerVm.
using System.ComponentModel.DataAnnotations;
namespace MvcApplication1.Models.ViewModels
{
public class PlayerVm
{
[Display (Name = "Player")]
public string Name { get; set; }
[Display (Name = "location")]
public string Position { get; set; }
}
}
The HomeController contains two controller methods: one is displayed in the drop-down box and the other is responded to the change event in the drop-down box.
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MvcApplication1.Models;
using MvcApplication1.Models.ViewModels;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
// Display the Team to the drop-down list
public ActionResult Index()
{
var teamVm = new TeamVm();
using (var context = new ClubModelContainer())
{
var teams = context.Team.ToList();
teamVm.Players = teams.Select(t => new SelectListItem()
{
Text = t.Name,
Value = t.ID.ToString()
});
}
return View(teamVm);
}
// Response to the drop-down list
public JsonResult PlayersByTeamID(int id)
{
IEnumerable<PlayerVm> playersVmList = new List<PlayerVm>();
using (var context = new ClubModelContainer())
{
var players = context.Player.Where(x => x.TeamID == id).ToList();
playersVmList = players.Select(p => new PlayerVm()
{
Name = p.Name,
Position = p.Position
});
}
return Json(playersVmList, JsonRequestBehavior.AllowGet);
}
}
}
Home/Index. cshtml view section:
● Defines the Mustache template. The placeholder is consistent with the json data.
● The drop-down box triggers the change event, obtains the json data returned by the background, fills in the Mustache template, and appends html content to the page.
@model MvcApplication1.Models.ViewModels.TeamVm
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div>
@Html.LabelFor(model => model.ID)
@Html.DropDownListFor(model => model.ID, Model.Players)
</div>
<div id="playersDiv">
</div>
@section scripts
{
<script src="~/Scripts/mustache.js"></script>
<script type="text/javascript">
$(function() {
$('#ID').change(function() {
var id = $('#ID').val();
var playersDiv = $('#playersDiv');
$.ajax({
cache: false,
type: "GET",
url: "@Url.Action("PlayersByTeamID","Home")",
data: { "id": id },
success: function (data) {
var result = "";
playersDiv.html('');
$.each(data, function (index, row) {
Var template = require ('{players'{.html (); // obtain the template's html
Var bookData = Mustache. render (template, row); // fill in the data of each row in the template to get html content
playersDiv.append(bookData);
});
},
error: function (xhr, ajaxOptions, thrownError) {
Alert ('loading failed ');
}
});
});
});
</script>
<script type="text/template" id="players">
<table>
<tr>
<Td> players: </td>
<td>{{Name}}</td>
</tr>
<tr>
<Td> location: </td>
<td>{{Position}}</td>
</tr>
</table>
</script>
}