Simple Xml document parsing and Vue rendering in MVC mode

Front-end code:

<script src="~/Js/jquery-3.3.1.min.js"></script>

<script src="~/Js/vue.js"></script>
</head>
<body>
<div>
<div id="vue_det">
<ul v-for="mess in message">
<li>{{mess.Code}}</li>
<li>{{mess.EnName}}</li>
<li>{{mess.Name}}</li>
<li>{{mess.Level}}</li>
</ul>
</div>
</div>
<script type="text/javascript">
var vm = new Vue({
el: '#vue_det',
data: {
message: []
}
})
$(function () {
$.ajax({
type: "POST",
url: "/Home/xml",
data: "",
success: function (aaa) {
console.log(aaa);
vm.message = aaa;
}
})
})
</script>
</body>

</html>

 

Controller code:

 

private static List<Domain> domains;
public static List<Domain> Domains
{
get
{
if (domains == null || domains.Count <= 0)
{
domains = new List<Domain>();
XmlDocument doc = new XmlDocument();
doc.Load(HttpRuntime.BinDirectory + "/Resourse/Domains.xml");
foreach (XmlNode node in doc.DocumentElement.ChildNodes)
{
Domain l = new Domain();
l.Code = Convert.ToInt64(node.Attributes["code"].Value);
l.Name = node.Attributes["name"].Value;
l.EnName = node.Attributes["enname"].Value;
l.Level = Convert.ToInt32(node.Attributes["level"].Value);
l.Demo = node.InnerText;//text }
domains.Add(l) in child nodes;

}
return domains;
}
}
public JsonResult xml()
{
return Json(Domains);
}

简单的Ajax请求,返回Json //xml文件内容

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324903977&siteId=291194637