用剃须刀在模型中循环 [英] looping through model with razor
本文介绍了用剃须刀在模型中循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试仅打印出每个电影项目的名称.
Trying to just print out the name of each movie item.
例如
- 黑暗骑士
我在剃刀代码中缺少某些声明性语句吗?在模型之后,Intellisense不会列出任何选项
Am I missing some declarative statement in the razor code? Intellisense doesn't list any options after Model
[XmlRoot("movies")]
public class MovieSummary
{
[XmlElement("movie")]
public List<Movie> Movies { get; set; }
}
public class Movie
{
public int id { get; set; }
public string name { get; set; }
}
public ActionResult Index()
{
MovieSummary summary = Deserialize();
return View(summary);
}
public static MovieSummary Deserialize()
{
using (TextReader reader = new StreamReader("c:\\movies.xml"))
{
XmlSerializer serializer = new XmlSerializer(typeof(MovieSummary));
return (MovieSummary)serializer.Deserialize(reader);
}
}
<?xml version="1.0" ?>
<movies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<movie>
<id>1</id>
<name>The Dark Knight</name>
</movie>
</movies>
<ul>
@if (Model != null) {
foreach (var movies in Model.Movies)
{
<li>@movies.movie.name</li>
}
}
</ul>
推荐答案
在View文件顶部,声明模型类型,如下所示:
At the top of your View file, declare your model type as follows:
@model MovieSummary
然后迭代您的电影
集合:
<ul>
@if (Model != null)
{
foreach (var movie in Model.Movies)
{
<li>@movie.name</li>
}
}
</ul>
这篇关于用剃须刀在模型中循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文