简体   繁体   English

XML输出模型以查看MVC4

[英]output model from xml to view MVC4

learning MVC/C# as I go, what's the most efficient way of serializing xml data to a model and then presenting (bind) that into a view? 我将继续学习MVC / C#,最有效的方法是将xml数据序列化为模型,然后将其呈现(绑定)到视图中?

I have the following 我有以下

 public static MovieSummary Deserialize()
 {
     XmlSerializer serializer = new XmlSerializer(typeof(MovieSummary));
     TextReader textReader;

     textReader = new StreamReader("c:\\movies.xml");

     MovieSummary summary = (MovieSummary)serializer.Deserialize(textReader); 
     textReader.Close();
     return summary;
 }

public class MovieSummary
{
    public List<Movie> Movies { get; set; }
}

public class Movie
{
    public int id { get; set; }
    public string name { get; set; }
}


 <?xml version="1.0" encoding="utf-8"?>
 <movies>
      <movie>
          <id>1</id>
          <name>The Dark Knight</name>
      </movie>
      <movie>
          <id>2</id>
          <name>Iron Man</name>
      </movie>
  </movies>

I'd like to call the deserialize function and consume the summary. 我想调用反序列化函数并使用摘要。 how would the code for the controller look for public ActionResult ListMovies()? 控制器的代码将如何查找公共ActionResult ListMovies()?

Call your function, then return the results to your view: 调用您的函数,然后将结果返回到您的视图:

public ActionResult ListMovies()
{
  MovieSummary summary = Deserialize();
  return View(summary);
}

Inside your view, you would reference your model and generate your HTML. 在视图内部,您将引用模型并生成HTML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM