Trying to just print out the name of each movie item.
eg.
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>
At the top of your View file, declare your model type as follows:
@model MovieSummary
Then iterate on your Movies
collection:
<ul>
@if (Model != null)
{
foreach (var movie in Model.Movies)
{
<li>@movie.name</li>
}
}
</ul>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With